Message402347
length_hint(), not len(). Its cost is included in microbenchmark for list(), where it is followed by iterating 2000 items.
Calling operator.length_hint() in Python:
$ ./python -m pyperf timeit -s 'it = iter(range(1000)); from operator import length_hint' 'length_hint(it)'
baseline: Mean +- std dev: 109 ns +- 6 ns
PR 27986: Mean +- std dev: 109 ns +- 5 ns
PR 28176: Mean +- std dev: 115 ns +- 5 ns
$ ./python -m pyperf timeit -s 'it = iter(range(0, 10**20, 3**35)); from operator import length_hint' 'length_hint(it)'
baseline: Mean +- std dev: 114 ns +- 6 ns
PR 27986: Mean +- std dev: 95.6 ns +- 4.3 ns
PR 28176: Mean +- std dev: 285 ns +- 13 ns
Indirect call from C (it includes overhead for calling list() and iter() in Python):
$ ./python -m pyperf timeit -s 'r = range(10**20, 10**20+1, 3**35)' 'list(iter(r))'
baseline: Mean +- std dev: 331 ns +- 16 ns
PR 27986: Mean +- std dev: 300 ns +- 16 ns
PR 28176: Mean +- std dev: 391 ns +- 18 ns
With few experiments I have found that PR 28176 is faster than PR 27986 for list(iter(range(...))) if a range is larger than 40-100 items. |
|
Date |
User |
Action |
Args |
2021-09-21 18:52:37 | serhiy.storchaka | set | recipients:
+ serhiy.storchaka, gvanrossum, rhettinger, lukasz.langa, Dennis Sweeney |
2021-09-21 18:52:37 | serhiy.storchaka | set | messageid: <1632250357.37.0.346018910014.issue45026@roundup.psfhosted.org> |
2021-09-21 18:52:37 | serhiy.storchaka | link | issue45026 messages |
2021-09-21 18:52:37 | serhiy.storchaka | create | |
|