【发布时间】:2011-06-12 14:48:47
【问题描述】:
我已经完成了一些基本的性能和内存消耗基准测试,我想知道是否有任何方法可以让事情变得更快......
我有一个包含 70,000 个元素的巨型列表,其中包含一个 numpy ndarray,以及该列表中元组中的文件路径。
我的第一个版本将列表的切片副本传递给 python 多进程模块中的每个进程,但它会使 ram 使用量激增至超过 20 GB
第二个版本我将它移动到全局空间并通过索引(例如 foo[i])在我的每个进程的循环中访问它,这似乎将它放入共享内存区域/CoW 语义中进程因此它不会爆炸内存使用量(保持在 ~3 GB)
但是,根据性能基准/跟踪,现在大部分应用程序时间似乎都花在“获取”模式...
所以我想知道是否有任何方法可以以某种方式将此列表转换为某种无锁/只读,以便我可以取消部分获取步骤以帮助加快访问速度。
编辑 1:这是应用分析的前几行输出
ncalls tottime percall cumtime percall filename:lineno(function)
65 2450.903 37.706 2450.903 37.706 {built-in method acquire}
39320 0.481 0.000 0.481 0.000 {method 'read' of 'file' objects}
600 0.298 0.000 0.298 0.000 {posix.waitpid}
48 0.271 0.006 0.271 0.006 {posix.fork}
编辑 2:这是列表结构的示例:
# Sample code for a rough idea of how the list is constructed
sim = []
for root, dirs, files in os.walk(rootdir):
path = os.path.join(root, filename)
image= Image.open(path)
np_array = np.asarray(image)
sim.append( (np_array, path) )
# Roughly it would look something like say this below
sim = List( (np.array([[1, 2, 3], [4, 5, 6]], np.int32), "/foobar/com/what.something") )
那么今后 SIM 列表将是只读的。
【问题讨论】:
标签: python performance numpy