[Python-Dev] PEP 309
Raymond Hettinger
python at rcn.com
Sun Feb 27 01:38:59 CET 2005
> I did a quick experiment:
>
> >python -m timeit -s "from operator import itemgetter; l=range(8)"
> "itemgetter(1)(l)"
> 1000000 loops, best of 3: 0.548 usec per loop
>
> >python -m timeit -s "l=range(8)" "(lambda x:x[1])(l)"
> 1000000 loops, best of 3: 0.597 usec per loop
>
> That's far less of a difference than I expected from itemgetter!
You've timed how long it takes to both construct and apply the retrieval
function. The relevant part is only the application:
C:\pydev>python -m timeit -r9 -s "from operator import itemgetter;
s=range(8); f=itemgetter(1)" "f(s)"
1000000 loops, best of 9: 0.806 usec per loop
C:\pydev>python -m timeit -r9 -s "s=range(8); f=lambda x:x[1]" "f(s)"
100000 loops, best of 9: 1.18 usec per loop
So the savings is about 30% which is neither astronomical, nor
negligible.
Raymond
More information about the Python-Dev
mailing list