python dict字典循环遍历性能 iteritems比items快

dict字典循环遍历,从测试结果看,iteritems比items快一个数量级 

In [32]: def add_dic(a, num):
    ...:     for i in range(num):
    ...:         a[i] = "value" + str(i)
    ...:

In [33]: def xun(a):
    ...:     print "begin"
    ...:     for key, value in a.items():
    ...:         continue
    ...:     print "over"
    ...:

In [34]: def xun1(a):
    ...:     print "begin"
    ...:     for key, value in a.iteritems():
    ...:         continue
    ...:     print "over"
    ...:

In [35]: add_dic(a, 1000000)

In [36]: time xun(a)
begin
over
CPU times: user 728 ms, sys: 0 ns, total: 728 ms
Wall time: 733 ms

In [37]: time xun1(a)
begin
over
CPU times: user 56 ms, sys: 0 ns, total: 56 ms
Wall time: 57.9 ms

python dict字典循环遍历性能 iteritems比items快_第1张图片

你可能感兴趣的:(python,python,字典,dict,遍历,items)