| Store | Cart

performance of dictionary lookup vs. object attributes

From: Fredrik Lundh <fred...@pythonware.com>
Fri, 25 Aug 2006 15:03:24 +0200
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> 

Recent Messages in this Thread
bear...@lycos.com Aug 25, 2006 12:15 pm
Aahz Aug 25, 2006 02:34 pm
Gabriel Genellina Aug 25, 2006 03:23 pm
s...@pobox.com Aug 25, 2006 03:33 pm
Patrick Maupin Aug 26, 2006 07:51 pm
s...@pobox.com Aug 26, 2006 08:28 pm
Carl Banks Aug 26, 2006 09:16 pm
Dieter Maurer Aug 28, 2006 07:26 pm
Patrick Maupin Aug 29, 2006 12:30 am
Jarek Zgoda Aug 26, 2006 08:35 pm
Patrick Maupin Aug 26, 2006 08:51 pm
Patrick Maupin Aug 26, 2006 08:49 pm
bear...@lycos.com Aug 25, 2006 05:38 pm
Peter Otten Aug 25, 2006 12:48 pm
Fredrik Lundh Aug 25, 2006 01:03 pm
Jacob Hallen Aug 27, 2006 11:59 pm
Patrick Maupin Aug 28, 2006 12:52 am
David Isaac Aug 28, 2006 03:49 am
Antoon Pardon Aug 29, 2006 07:07 am
Messages in this thread