enumerate is ~1.45× slower than range(len())
`enumerate` is ~1.5× faster than `range(len(xs))` because it avoids indexing.
Result
| Run | range(len()) | enumerate | Ratio |
|---|---|---|---|
| Run 1 | 5.353 | 7.862 | 0.68× |
| Run 2 | 5.295 | 7.747 | 0.68× |
| Run 3 | 5.533 | 7.678 | 0.72× |
Spread over 3 runs: range(len()) CV 1.9% / enumerate CV 1.0% / Ratio CV 2.6%
Conditions
Walking 100,000 elements while using both index and value.
Where this number does not apply
Reverse or step iteration still needs an index.
Reproduce
cd bench && python micro_py.py --out ../data/run.json --samples 21