【发布时间】:2014-01-14 03:52:23
【问题描述】:
我有以下代码:
keywordindex = cPickle.load(open('keywordindex.p','rb'))#contains~340 thousand items
masterIndex = {}
indexes = [keywordindex]
for partialIndex in indexes:
start = time.time()
for key in partialIndex.iterkeys():
if key not in masterIndex.keys():
masterIndex[key]= partialIndex[key]
elif key in masterIndex.keys():
masterIndex[key].extend(partialIndex[key])
cPickle.dump(masterIndex,open('MasterIndex.p','wb'))
print int(time.time() - start), ' seconds'#every thousand loops
当循环运行时,我正在经历性能下降,前 10000 次大约需要 5 秒/1000 秒,但每 10000 次左右需要另外 1 秒,直到它需要 3 倍的时间。我试图以各种可能的方式简化代码,但我似乎无法弄清楚是什么原因造成的。是否有一个原因?这不是内存问题,我只有 30% 的使用率
【问题讨论】:
-
你为什么要创建一个单元素列表
indexes然后循环遍历它?看起来你可以完全废弃外循环。 -
@user 有超过 1 个列表,我为问题删除了它们
标签: python dictionary pickle