【问题标题】:Python saving to disk getting slower after every loop每次循环后Python保存到磁盘的速度变慢
【发布时间】:2016-06-14 20:53:07
【问题描述】:

通过 numpy.save() 保存在 Linux 系统上超过 1000 个循环会变慢。是什么导致了这个问题?

from time import time
import numpy as np

arr = np.ones([1080, 1920, 3], dtype=np.uint8)
path = "/tmp/testArray.{0}"

runrange = range(1000)

for i in runrange :
    t = time()
    np.save(path.format(i), arr)
    print time()-t

它从 0.011 开始,然后达到 0.106 并最终达到 0.19 左右

【问题讨论】:

  • 目录是一个链表。在目录中找到新文件的位置需要时间,并且目录中的文件越多,需要的时间越长。
  • @kindall 我认为你应该回答这个问题 :)。
  • @kindall 在使用 numpy tofile 循环并将 1000 多个数组写入单个打开的文件时,我得到了同样的减速。文件超过 1.5gigs 后,查找文件或写入更多数组所需的时间从 0.01 跳到 0.12
  • 我在使用 pickle 时遇到了同样的问题

标签: python linux file numpy io


【解决方案1】:

我知道这是一篇古老的帖子。但是,我无法重现问题行为 - 如果有的话,它会随着时间的推移变得稍微快一些。

from time import time

from matplotlib import pyplot as plt
import numpy as np

arr = np.ones((1080, 1920, 3), dtype=np.uint8)
path = "testArray.{0}"
times = []

for i in range(1_000):
    t = time()
    np.save(path.format(i), arr)
    times.append((time() - t) * 1_000)

plt.figure(figsize=(9, 9))
plt.plot(range(1_000), times)
plt.savefig('time.png')

使用Python 3.9.1numpy==1.20.1在Win10上测试

【讨论】:

    猜你喜欢
    • 2020-01-05
    • 1970-01-01
    • 2011-08-16
    • 2018-04-30
    • 2020-01-24
    • 2022-09-27
    • 1970-01-01
    • 2021-01-10
    • 2019-04-05
    相关资源
    最近更新 更多