"optimizing out" getattr

Daishi Harada daishi at gmail.com
Thu Sep 15 19:55:50 EDT 2005


Peter Otten wrote:
> No, you can just sit back and wait -- for Python 2.5:

Thanks for the tip;
Although for my current use
I can't target 2.5, I hadn't even
noticed the attr/itemgetter
additions to operator in 2.4,
so I appreciate the pointer
for future reference.
d

> $ cat attr_tuple25.py
> import operator
>
> class A:
>     a = 1
>     b = 2
>
> get2 = operator.attrgetter("a", "b")
>
> def get1(x):
>     return x.a, x.b
>
> $ python2.5 -m timeit -s'from attr_tuple25 import A, get1, get2' 'get1(A)'
> 1000000 loops, best of 3: 0.813 usec per loop
> $ python2.5 -m timeit -s'from attr_tuple25 import A, get1, get2' 'get2(A)'
> 1000000 loops, best of 3: 0.495 usec per loop
> 
> Time till release is not included in the timings :-)
> 
> Peter




More information about the Python-list mailing list