I am wondering about the %timeit command in IPython
From the docs:
%timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_codeOptions:
-n: execute the given statement times in a loop. If this value is not given, a fitting value is chosen.
-r: repeat the loop iteration times and take the best result. Default: 3
For example, if I write:
%timeit -n 250 -r 2 [i+1 for i in range(5000)]
So, -n 250 executes [i+1 for i in range(5000)] 250 times? Then what does -r 2?
It specifies the number of repeats, the number of repeats are used to determine the average. For example:
The number of executions will be
n * rbut the statistic is based on the number ofrepeats(r) but the number of "loops" for each repeat is determined based on thenumber(n).Basically you need a large enough
nso the minimum of the number of loops is accurate "enough" to represent the fastest possible execution time, but you also need a large enoughrto get accurate "statistics" on how trustworthy that "fastest possible execution time" measurement is (especially if you suspect that some caching could be happening).For superficial timings you should always use an
rof3,5or7(in most cases that's large enough) and choosenas high as possible - but not too high, you probably want it to finish in a reasonable time :-)