sum(list(range(n))) is ~1.78× slower than sum(range(n))
`sum(range(n))` is ~1.8× faster than `sum(list(range(n)))` — building the list costs the difference.
Result
| Run | sum(range(n)) | sum(list(range(n))) | Ratio |
|---|---|---|---|
| Run 1 | 17.008 | 31.215 | 0.54× |
| Run 2 | 17.465 | 30.683 | 0.57× |
| Run 3 | 18.552 | 31.157 | 0.60× |
Spread over 3 runs: sum(range(n)) CV 3.7% / sum(list(range(n))) CV 0.8% / Ratio CV 3.6%
Conditions
One million elements.
Where this number does not apply
If the list already exists, this gap is irrelevant.
Reproduce
cd bench && python micro_py.py --out ../data/run.json --samples 21