$ python -m perf timeit --rigorous --duplicate 10 --hist --stats -s 'import collections; Point = collections.namedtuple("Point", "x y")' 'Point(10.5, 11.5)'
$ python -m perf timeit --rigorous --duplicate 10 --hist --stats '{"x": 10.5, "y": 11}'
Point(10.5, 11.5)
is 6 times slower than {'x': 10.5, 'y':11.5}
.
The absolute times are 635 ns +- 26 ns
vs. 105 ns +- 4 ns
. Don't
create classes at the function level unless you know why you need
it. If your API requires dict
than use dict
-- it has nothing to
do with performance.