Serhiy Storchaka <storchaka+cpyt...@gmail.com> added the comment:
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. ---------- _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue45026> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com