[Python-ideas] namedtuple with ordereddict
Alexander Belopolsky
alexander.belopolsky at gmail.com
Wed Jul 19 14:35:08 EDT 2017
On Wed, Jul 19, 2017 at 12:10 PM, Giampaolo Rodola' <g.rodola at gmail.com>
wrote:
> Should have been something like this instead:
>
> $ python3.7 -m timeit -s "import collections; Point =
> collections.namedtuple('Point', ('x', 'y')); x = [5, 1]" "Point(*x)"
> 1000000 loops, best of 5: 311 nsec per loop
>
> $ python3.7 -m timeit -s "x = [5, 1]" "tuple(x)"
> 5000000 loops, best of 5: 89.8 nsec per loop
>
This looks like a typical python function call overhead. Consider a toy
class:
$ cat c.py
class C(tuple):
def __new__(cls, *items):
return tuple.__new__(cls, items)
Comparing to a naked tuple, creation of a C instance is more than 3x slower.
$ python3 -m timeit -s "from c import C; x = [1, 2]" "C(*x)"
1000000 loops, best of 3: 0.363 usec per loop
$ python3 -m timeit -s "x = [1, 2]" "tuple(x)"
10000000 loops, best of 3: 0.114 usec per loop
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170719/c21abdb7/attachment.html>
More information about the Python-ideas
mailing list