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 _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com