[Python-Dev] Impact of Namedtuple on startup time
Steven D'Aprano
steve at pearwood.info
Mon Jul 17 21:22:48 EDT 2017
On Tue, Jul 18, 2017 at 01:17:24AM +0200, Giampaolo Rodola' wrote:
> The extra memory overhead is a price I would be happy to pay considering
> that collections.namedtuple is considerably slower than a plain tuple.
> Other than the additional overhead on startup / import time, instantiation
> is 4.5x slower than a plain tuple:
>
> $ python3.7 -m timeit -s "from collections import namedtuple; nt =
> namedtuple('xxx', ('x', 'y'))" "nt(1, 2)"
> 1000000 loops, best of 5: 313 nsec per loop
>
> $ python3.7 -m timeit "tuple((1, 2))"
> 5000000 loops, best of 5: 68.4 nsec per loop
I don't think that is a fair comparision. As far as I can tell, that
gets compiled to a name lookup for "tuple" which then returns its
argument unchanged, the tuple itself being constant-folded at compile
time.
py> dis.dis("tuple((1, 2))")
1 0 LOAD_NAME 0 (tuple)
3 LOAD_CONST 2 ((1, 2))
6 CALL_FUNCTION 1 (1 positional, 0 keyword pair)
9 RETURN_VALUE
--
Steve
More information about the Python-Dev
mailing list