Why __slots__ slows down attribute access?

J

Jack

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.
__slots__ are implemented at the class level by creating descriptors
(Implementing Descriptors) for each variable name. It makes a little
bit slower.

So __slots__ just saves memory space by preventing creation of
__dict__ and __weakref__ on each instance, while sacrifice performance
and inheritance flexibility.
http://groups.google.com/group/comp.lang.python/msg/6623e8b94b6d6934


D:\>d:\python-v3.1.2\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.237 usec per loop

D:\>d:\python-v3.1.2\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.214 usec per loop

D:\>d:\python-v2.6.4\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.26 usec per loop

D:\>d:\python-v2.6.4\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.217 usec per loop
 
P

Peter Otten

Jack said:
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?
 
A

Adam Skutt

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.

No, they don't really show anything. The default clocks used by
timeit lack the resolution to measure such things accurately; you're
measuring various noise sources on your system. The range of 100
trials of 1million iterations on my system is 70.3 ms, which is 70ns
when divided by a million, which is about the size of the difference
you show. A 70ns average difference between iterations is trivially
attributable to noise on a modern machine.

Run enough trials or just wait for the moon to move a bit, and I
wouldn't be terribly surprised if you got difference results.
Rebooting your machine might be enough to do it.

Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top