4 ms·
You're measuring the wrong thing. itemgetter() is supposed to be called just once and it's the returned function that is used in the loop/map. It should be:
by andreasvc 14y ago
You're measuring the wrong thing. itemgetter() is supposed to be called just once and it's the returned function that is used in the loop/map. It should be:
python -m timeit 'import string;from operator import itemgetter; getter=itemgetter('slice'); [getter(x) for x in string.letters]'
- benhoyt 14y agoTrue. But you should also put the setup in a -s option, because you're not timing the imports. And you don't really need the loop, because timeit does a bunch of loops for you: >python -m timeit -s "from operator import itemgetter; elem0 = itemgetter(0)" "elem0('s')" 10000000 loops, best of 3: 0.095 usec per loop >python -m timeit -s "from operator import itemgetter; elem0 = lambda x: x[0]" "elem0('s')" 10000000 loops, best of 3: 0.138 usec per loop