__slots__ are used to conserve memory when creating lots of objects, not CPU time. (Instead of every object having a __dict__, all attributes are stored in a small array and accessed using properties set on the class.)
namedtuple is slower than even a class without __slots__, by the way. It operates by mapping attribute names to indices (there's a reason it's called a "named tuple", not "struct" or something similar), doing twice the number of lookups for every dot.
Right, I knew slots was to conserve memory primarily, but shouldn't that increase performance as a result? I'd expect less memory management to mean quicker access time when modifying, deleting, creating and garbage collection. But certainly better memory performance.
I thought named tuples were quicker than classes without slots defined... You're positive about that?
In response to your deleted comment, I didn't waste all that time for nothing =)
Nope, just tested, it's very slightly slower than tuple index access, but just like it about twice as fast as nameduple name access.
The stuff looks like this here:
index access takes about 40ns
name lookup takes about 45ns both for usual classes and those with __slots__, in fact slots are a tiniest bit slower.
namedtuple lookup by name takes about 115ns
To be honest, I can't say how exactly it works out to these numbers, I'd say that the only way to really be sure is to run this stuff under a C profiler. That could be a pretty useful experience in itself
From what I can tell from grepping through the code in Vim, it's pretty much a coincidence that the first two things take the same time.
Index access goes through a bunch of pure-C redirects until it hits tuplesubscript which casts the index to size_t and fetches the value from the object itself.
Class lookup by name IIRC does two unsuccessful dictionary lookups in the class and object attributes, then a successful lookup in the instance dictionary. Slots lookup should do a successful dictionary lookup in the class dictionary then indirectly call a C function that fetches shit by index or something.
Namedtuple lookup by name probably involves a pure Python function call, which is slooooow.
You don't want to do that in Python if performance is critical.
Adding three indexed items is not performance-critical.
If you have a million+ items, then you install numpy and put your items into a numpy.ndarray, and then vectorize your operations. Like, if you want to add two arrays, you write a + b (instead of for i, it in a: result.append(it + b[i])) and the underlying library written in Fortran very efficiently does what you meant.
I always considered numpy to be scientists' tools, but I never really thought about how it's fortran under the hood and how it might be higher performance for certain things like that. a+b looks a lot cleaner as well.
46
u/robin-gvx Jun 23 '14
I have problems with some of these.
Replacing
a or b or cbyany([a, b, c]). Is that really more Pythonic? If they'd been in an iterable already I'd say yes, but now not so much.Catching
KeyErrorjust to raise anotherKeyError? Just let it bubble up.I kinda hate
namedtuplebecause it's such a hack, but maybe that's just me.Also, I'd say the opposite of Pythonic code is not normal code. Un-Pythonic or unidiomatic, or maybe something about newbies.