[New-bugs-announce] [issue17194] operator.attrgetter is slow

Florent Xicluna report at bugs.python.org
Tue Feb 12 17:43:15 CET 2013


New submission from Florent Xicluna:

When two implementations give the same result, I use to run micro benchmarks to give me an hint.

I just noticed that attrgetter is slower than a lambda here:

$ python3.3 -m timeit -s 'from operator import attrgetter; n1 = attrgetter("__name__"); n2 = lambda s: s.__name__' 'rv = n1(int)'
1000000 loops, best of 3: 0.275 usec per loop
$ python3.3 -m timeit -s 'from operator import attrgetter; n1 = attrgetter("__name__"); n2 = lambda s: s.__name__' 'rv = n2(int)'
1000000 loops, best of 3: 0.347 usec per loop

(verified with 2.6, 2.7 and 3.3. But for 2.5 attrgetter is faster)


The function operator.itemgetter does not have same issue.
$ python3.3 -m timeit -s 'from operator import itemgetter; n1 = itemgetter("foot"); n2 = lambda s: s["foot"]; d = {"foot": 42}' 'rv = n1(d)'
10000000 loops, best of 3: 0.122 usec per loop
$ python3.3 -m timeit -s 'from operator import itemgetter; n1 = itemgetter("foot"); n2 = lambda s: s["foot"]; d = {"foot": 42}' 'rv = n2(d)'
10000000 loops, best of 3: 0.176 usec per loop

----------
messages: 181967
nosy: flox
priority: low
severity: normal
status: open
title: operator.attrgetter is slow
type: performance

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17194>
_______________________________________


More information about the New-bugs-announce mailing list