On Wed, Jul 19, 2017 at 12:10 PM, Giampaolo Rodola' <g.rod...@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
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to