Why does python's timeit use the 'best of 3' to measure the time elapsed?
Asked Answered
B

1

15

I do not see the rationale why python's timeit module measures the time using the best of 3. Here is an example from my console:

~ python -m timeit 'sum(range(10000))'
10000 loops, best of 3: 119 usec per loop

Intuitively, I would have put the whole time together then divide it by the number of loops. What is the intuition of picking up the best of 3 among all loops? It seems just a bit unfair.

Barcus answered 28/12, 2015 at 19:46 Comment(1)
There's random cpu processes that go on that slow down the speed. Theoretically, it should take the same amount of time each run through, so it chooses the fastest that it can do.Beaudoin
T
10

As noted in the documentation:

default_timer() measurations can be affected by other programs running on the same machine, so the best thing to do when accurate timing is necessary is to repeat the timing a few times and use the best time. The -r option is good for this; the default of 3 repetitions is probably enough in most cases.

Trackless answered 28/12, 2015 at 19:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.