Python 迭代dict 效率

迭代dict也要讲求效率,不然就要走进性能陷阱

以下三种迭代方式:keys,iterkeys, hashkey

 

import timeit

DICT_SIZE = 100 * 100000

testDict = dict()
for i in xrange(DICT_SIZE):
    testDict[i] = i

assert len(testDict) == DICT_SIZE

def test1():
    for _ in testDict.keys():
        pass

def test2():
    for _ in testDict.iterkeys():
        pass

def test3():
    for _ in testDict:
        pass

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)

结果:

4198.55953097
1695.41651607
1896.270154

keys()会生成一个list,耗内存又耗时间;iterkeys是迭代key,不会生成list。第二和第三方法相差不多。

转载于:https://www.cnblogs.com/zhuangzebo/p/4320194.html

你可能感兴趣的:(Python 迭代dict 效率)