Google colab performance throttled after a certain amount of iterations

Viewed 440

I have the following code to iterate over 6000 elements, open and parse 3 files associated with each element.

For some reason, after a certain amount of iterations, the performance is throttled to around 1 iteration a second...

def read_ColorHistogram(fname):
    """Scan Color Histogram from file
    Input file contains RGB histogram,
    Return a matrix of (3,256)"""
    RGB_Hist = np.zeros((3,256))
    with open(fname) as f:
        i_l = 0 # line index
        for line in f:
            pairs = line.split()
            hist_dict = {int(p.split(':')[0]):float(p.split(':')[1]) for p in pairs}
            for idx in hist_dict.keys():
                RGB_Hist[i_l,idx] = hist_dict[idx]
            i_l += 1
    return RGB_Hist

i = 0
# For each video in set
for video in labels["video"]:

  start = time.time()

  video_name = video.split('.')[0]

  frame_1_path = f'./Dev-set/ColorHistogram/{video_name}-0.txt'
  frame_2_path = f'./Dev-set/ColorHistogram/{video_name}-56.txt'
  frame_3_path = f'./Dev-set/ColorHistogram/{video_name}-112.txt'

  # Load colours for frame
  frame_1_colours = read_ColorHistogram(frame_1_path)
  frame_2_colours = read_ColorHistogram(frame_2_path)
  frame_3_colours = read_ColorHistogram(frame_3_path)

  stop = time.time()
  duration = stop-start
  print(f'{i} : {duration}')
  i += 1

Here is the output, you can see at iteration 331 - the performance drops off and it slows to 1+ second for each loop.

My for loop contains 6000 elements so this performance isn't acceptable...

320 : 0.020931243896484375
321 : 0.023003816604614258
322 : 0.023036956787109375
323 : 0.01930379867553711
324 : 0.01989912986755371
325 : 0.019346237182617188
326 : 0.01939535140991211
327 : 0.019335508346557617
328 : 0.019719600677490234
329 : 0.020183801651000977
330 : 0.018976211547851562
331 : 1.3781886100769043
332 : 1.0635037422180176
333 : 1.0903840065002441
334 : 1.5412299633026123
335 : 1.2986907958984375
336 : 1.1264688968658447
0 Answers
Related