This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients Dennis Sweeney, gvanrossum, lukasz.langa, rhettinger, serhiy.storchaka
Date 2021-09-21.18:52:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1632250357.37.0.346018910014.issue45026@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2021-09-21 18:52:37serhiy.storchakasetrecipients: + serhiy.storchaka, gvanrossum, rhettinger, lukasz.langa, Dennis Sweeney
2021-09-21 18:52:37serhiy.storchakasetmessageid: <1632250357.37.0.346018910014.issue45026@roundup.psfhosted.org>
2021-09-21 18:52:37serhiy.storchakalinkissue45026 messages
2021-09-21 18:52:37serhiy.storchakacreate