Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 959
zell Avatar asked Dec 28 '15 19:12

zell


1 Answers

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.

like image 80
Simeon Visser Avatar answered Oct 07 '22 17:10

Simeon Visser