performance of dictionary lookup vs. object attributes

Andre Meyer meyer at acm.org
Fri Aug 25 09:03:51 EDT 2006


Good points! It's always good to learn from the pros!

So, what it means is that the test is not meaningful, because of the
different way that object attributes are accessed (not as o.x, which
could be compiled).

Nevertheless, the general impression remains that dicts *are* faster
than objects, because attribute lookup uses dicts itself. Is that
correct?

thanks
Andre


On 8/25/06, Fredrik Lundh <fredrik at pythonware.com> wrote:
> Andre Meyer wrote:
>
> > Is the test meaningful and are you surprised by the results?
>
> surprised by the amount of code you needed to test this, at least.  and you
> might wish to use the proper spelling for
>
>                 v = self.obj.__getattribute__(a)
>
> which is
>
>                 v = getattr(obj, a)
>
> and is quite a bit faster, at least in CPython.
>
> (you should also use setattr() instead of __setattr__; in general, if you find your-
> self *calling* a method named __method__, there's most likely a better way to
> do it).
>
> > I am, actually, because I would have assumed that attribute access with an
> > object should be faster because lookup can be precompiled.
>
> huh?  you're using a reflection API; there's no way the compiler can figure out
> in advance what you're going to pass to getattr().
>
> </F>
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
Dr. Andre P. Meyer                        http://python.openspace.nl/meyer
TNO Defence, Security and Safety          http://www.tno.nl/
Delft Cooperation on Intelligent Systems  http://www.decis.nl/

Ah, this is obviously some strange usage of the word 'safe' that I
wasn't previously aware of. - Douglas Adams



More information about the Python-list mailing list