"optimizing out" getattr
Peter Otten
__peter__ at web.de
Thu Sep 15 04:00:12 EDT 2005
Daishi Harada wrote:
> I'd like to get the 'get2' function below to
> perform like the 'get1' function (I've included
> timeit.py results).
> labels = ('a', 'b')
> def get1(x):
> return (x.a, x.b)
> def mkget(attrs):
> def getter(x):
> return tuple(getattr(x, label) for label in attrs)
> return getter
> get2 = mkget(labels)
>
> # % timeit.py -s "import test" "test.get1(test.a)"
> # 1000000 loops, best of 3: 0.966 usec per loop
> # % timeit.py -s "import test" "test.get2(test.a)"
> # 100000 loops, best of 3: 4.46 usec per loop
> I'm not sure how to write 'mkget' to do achieve
> this, however, except to use 'exec' - is that what
> would be necessary?
No, you can just sit back and wait -- for Python 2.5:
$ 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