performance of dictionary lookup vs. object attributes
Fredrik Lundh
fredrik at pythonware.com
Fri Aug 25 09:03:24 EDT 2006
Peter Otten wrote:
> $ python -m timeit -s'class A(object): pass' -s 'a = A(); a.alpha = 42'
> 'a.__getattribute__("alpha")'
> 1000000 loops, best of 3: 0.674 usec per loop
also:
timeit -s "class A(object): pass" -s "a = A(); a.alpha = 42"
"a.__getattribute__('alpha')"
1000000 loops, best of 3: 0.592 usec per loop
timeit -s "class A(object): pass" -s "a = A(); a.alpha = 42"
"getattr(a, 'alpha')"
1000000 loops, best of 3: 0.403 usec per loop
which is better, but nowhere close to
timeit -s"class A(object): pass" -s "a = A(); a.alpha = 42" "a.alpha"
10000000 loops, best of 3: 0.158 usec per loop
timeit -s"class A(object): pass" -s "a = A(); a.alpha = 42; d=vars(a)"
"d['alpha']"
10000000 loops, best of 3: 0.126 usec per loop
on the other hand, when we're talking about sub-microsecond operations, you'll
see "massive slowdowns" as soon as you're actually trying to do something with
the data you just fetched...
</F>
More information about the Python-list
mailing list