What is the advantage of iteritems?

后端 未结 5 1104
独厮守ぢ
独厮守ぢ 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:22

    as opposed to using the system time command, running in ipython with timeit yields:

    d = {i:i*2 for i in xrange(10**7*3)} #WARNING: it takes time and consumes a lot of RAM
    
    timeit for k in d: k, d[k]
    1 loops, best of 3: 2.46 s per loop
    
    timeit for k, v in d.iteritems(): k, v
    1 loops, best of 3: 1.92 s per loop
    

    i ran this on windows, python 2.7.6. have you run it multiple times to confirm it wasn't something going on with the system itself?

提交回复
热议问题