Why __slots__ slows down attribute access?

Peter Otten __peter__ at web.de
Tue Aug 23 12:26:50 CEST 2011


Jack wrote:

> People have illusion that it is faster to visit the attribute defined
> by __slots__ .
> http://groups.google.com/group/comp.lang.python/msg/c4e413c3d86d80be
> 
> That is wrong. The following tests show it is slower.

Not so fast. Here's what I get (python2.6.4, 64 bit):

$ python  -mtimeit -s "class A(object): __slots__ = ('a', 'b', 'c')" -s 
"inst = A()" "inst.a=5; inst.b=6; inst.c=7"
1000000 loops, best of 3: 0.324 usec per loop

$ python  -mtimeit -s "class A(object): pass" -s "inst = A()" "inst.a=5; 
inst.b=6; inst.c=7"
1000000 loops, best of 3: 0.393 usec per loop

Now what?




More information about the Python-list mailing list