3 ms·
Namedtuple CPU-speed performance for some common operations is TERRIBLE compared to dictionaries, at least on 2.7. Orders of magnitude difference. So bad that i
by tcoder 11y ago
Namedtuple CPU-speed performance for some common operations is TERRIBLE compared to dictionaries, at least on 2.7. Orders of magnitude difference. So bad that it really matters. It's shocking how bad the implementors got this.
- dangoldin 11y agoCurious what the operations you're talking about are. Get?
- pyre 11y agoIf you look at the the StackOverflow in my sibling comment to yours, running 'obj.attrname' on a namedtuple takes longer because it needs to translate 'attrname' to an integer index and then run (e.g.) 'obj[0]'. Outside of that, I'm not sure.
- tveita 11y agoPickling, for one. >>> nt = namedtuple('nt', 'a b') >>> nt10k = [nt(1, 2) for i in range(10000)] >>> dict10k = [{'a':1, 'b':2} for i in range(10000)] >>> timeit pickle.dumps(nt10k) 10 loops, best of 3: 26.3 ms per loop >>> timeit pickle.dumps(dict10k) 100 loops, best of 3: 2.49 ms per loop
- pyre 11y agonamedtuple is just a thin wrapper around tuple. It literally constructs a class definition that sub-classes tuple as a string and then executes it. You can see the string template that it uses here[1]. If you're interested in something like namedtuple there are other[2] things[3] you can use depending on your use-case. [1] https://github.com/python/cpython/blob/master/Lib/collections/__init__.py#L304 https://github.com/python/cpython/blob/master/Lib/collection... [2] http://stackoverflow.com/a/2648186 http://stackoverflow.com/a/2648186 [3] https://pypi.python.org/pypi/frozendict/ https://pypi.python.org/pypi/frozendict/
- Redoubts 11y agoIt's really weird that immutable tuples take a performance hit, compared to mutable lists (almost 50% on access!?!).
- pyre 11y agoThat's because Python 2 has a special case for lists[1]. This does not exist in Python 3[2] (and therefore neither does the performance difference). [1] https://github.com/python/cpython/blob/2.7/Python/ceval.c#L1521-L1545 https://github.com/python/cpython/blob/2.7/Python/ceval.c#L1... [2] https://github.com/python/cpython/blob/master/Python/ceval.c#L1591-L1601 https://github.com/python/cpython/blob/master/Python/ceval.c...
- Redoubts 11y agoIt looks like list access got slower as a result though.