Alex> Not sure what's a "small dict" in your world -- here, for example:

I would say "small" for us is typically fewer than ten keys.  We rarely have
dictionaries with dozens or hundreds of keys.

    Alex> python2.4 -mtimeit -s'd=dict.fromkeys(range(23))' 'for k, v in 
d.iteritems(): pass'
    Alex> 100000 loops, best of 3: 2.81 usec per loop

    Alex> python2.4 -mtimeit -s'd=dict.fromkeys(range(23))' 'for k, v in 
d.items(): pass'
    Alex> 100000 loops, best of 3: 4.82 usec per loop

We're still on 2.3 at work.  (All the people who have begun using
iteritems() have never used 2.4.)  Running your tests w/ 2.3 I get:

    ink:% timeit -s'd=dict.fromkeys(range(23))' 'for k, v in d.items(): pass'
    100000 loops, best of 3: 7.16 usec per loop
    ink:% type python
    python is hashed (/opt/lang/bin/python)
    ink:% timeit -s'd=dict.fromkeys(range(23))' 'for k, v in d.iteritems(): 
pass'
    100000 loops, best of 3: 6.83 usec per loop

Hardly seems worth the effort to type the extra four letters.  In fact, just
iterating over the dict's keys and assigning to v (my personally preferred
idiom) is faster:

    ink:% timeit -s'd=dict.fromkeys(range(23))' 'for k in d: v=d[k]'
    100000 loops, best of 3: 5.03 usec per loop

Sticking the 2.4 directory in front of PATH, I get:

    ink:% timeit -s'd=dict.fromkeys(range(23))' 'for k, v in d.items(): pass'
    100000 loops, best of 3: 6.49 usec per loop 
    ink:% timeit -s'd=dict.fromkeys(range(23))' 'for k, v in d.iteritems(): 
pass'
    100000 loops, best of 3: 4.16 usec per loop
    ink:% timeit -s'd=dict.fromkeys(range(23))' 'for k in d: v = d[k]' 100000
    loops, best of 3: 4.58 usec per loop

Not as dramatic an improvement as you saw, but yes, I'm surprised that
iteritems() is faster than items().  I stand corrected.

Thx,

Skip
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe: 
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com

Reply via email to