最近项目中使用到了dict的遍历,笔者写了几年的python,大多数都是使用dict.keys()的遍历方式。无奈项目执行过程中当dict中的元素上千万的时候,两层for循环性能实在是扛不住,于是测试了一下几种遍历方法的性能
import timeit
DICT_SIZE = 5000
testDict = dict()
for i in range(DICT_SIZE):
testDict[i] = i
assert len(testDict) == DICT_SIZE
def test1():
for k in testDict.keys():
key = k
value = testDict[k]
def test2():
for k in testDict:
key = k
value = testDict[k]
def test3():
for k in testDict.iterkeys():
key = k
value = testDict[k]
def test4():
for k,v in testDict.items():
key = k
value = v
print timeit.timeit("test1()", setup="from __main__ import test1", number=2000)
print timeit.timeit("test2()", setup="from __main__ import test2", number=2000)
print timeit.timeit("test3()", setup="from __main__ import test3", number=2000)
print timeit.timeit("test4()", setup="from __main__ import test4", number=2000)
测试结果如下:
0.698087930679 0.643119812012 0.642452955246 0.88481593132