What is the advantage of iteritems?

后端 未结 5 1167
独厮守ぢ
独厮守ぢ 2021-02-04 01:03

I am using Python 2.7.5 @ Mac OS X 10.9.3 with 8GB memory and 1.7GHz Core i5. I have tested time consumption as below.

d = {i:i*2 for i in xrange(10**7*3)} #WARN         


        
5条回答
  •  轮回少年
    2021-02-04 01:48

    dict.iter() wins out heavily in python 3.5.

    Here is a small performance stat:

    d = {i:i*2 for i in range(10**3)}
    timeit.timeit('for k in d: k,d[k]', globals=globals())
    75.92739052970501
    timeit.timeit('for k, v in d.items(): k,v', globals=globals())
    57.31370617801076 
    

提交回复
热议问题