经过一定数量的迭代后,Google colab的性能受到限制

2024-10-01 00:33:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码来迭代6000个元素,打开并解析与每个元素相关的3个文件

出于某种原因,经过一定数量的迭代后,性能会被限制在每秒1次迭代左右

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

这是输出,您可以在迭代331中看到-性能下降,每个循环的速度减慢到1秒以上

我的for循环包含6000个元素,因此此性能不可接受

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

Tags: pathnamein元素forreadtimevideo