Message298499
Benchmark comparing collections.namedtuple to structseq, to get an attribute:
* Getting an attribute by name (obj.attr):
Mean +- std dev: [name_structseq] 24.1 ns +- 0.5 ns -> [name_namedtuple] 45.7 ns +- 1.9 ns: 1.90x slower (+90%)
* Getting an attribute by its integer index (obj[0]):
(not significant)
So structseq is 1.9x faster than namedtuple to get an attribute by name.
haypo@speed-python$ ./bin/python3 -m perf timeit -s "from collections import namedtuple; Point=namedtuple('Point', 'x y'); p=Point(1,2)" "p.x" --duplicate=1024 -o name_namedtuple.json
Mean +- std dev: 45.7 ns +- 1.9 ns
haypo@speed-python$ ./bin/python3 -m perf timeit -s "from collections import namedtuple; Point=namedtuple('Point', 'x y'); p=Point(1,2)" "p[0]" --duplicate=1024 -o int_namedtuple.json
Mean +- std dev: 17.6 ns +- 0.0 ns
haypo@speed-python$ ./bin/python3 -m perf timeit -s "from sys import flags" "flags.debug" --duplicate=1024 -o name_structseq.json
Mean +- std dev: 24.1 ns +- 0.5 ns
haypo@speed-python$ ./bin/python3 -m perf timeit -s "from sys import flags" "flags[0]" --duplicate=1024 -o int_structseq.json
Mean +- std dev: 17.6 ns +- 0.2 ns
---
Getting an attribute by its integer index is as fast as tuple:
haypo@speed-python$ ./bin/python3 -m perf timeit --inherit=PYTHONPATH -s "p=(1,2)" "p[0]" --duplicate=1024 -o int_tuple.json
.....................
Mean +- std dev: 17.6 ns +- 0.0 ns |
|
Date |
User |
Action |
Args |
2017-07-17 13:01:03 | vstinner | set | recipients:
+ vstinner, rhettinger, ncoghlan, pitrou, eric.smith, methane, serhiy.storchaka, xiang.zhang, JelleZijlstra |
2017-07-17 13:01:03 | vstinner | set | messageid: <1500296463.44.0.260677862524.issue28638@psf.upfronthosting.co.za> |
2017-07-17 13:01:03 | vstinner | link | issue28638 messages |
2017-07-17 13:01:03 | vstinner | create | |
|