IPython% timeit, what is loop and iteration in parameters?

I'm curious about the %timeit in IPython

From docs :

 %timeit [-n<N> -r<R> [-t|-c] -q -p<P> -o] setup_code 

Options:

-n: execute specified operator times in a loop. If this value is not specified, the appropriate value is selected.

-r: repeat the loop iteration time and get 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 does [i+1 for i in range(5000)] 250 times? Then what is -r 2 ?

+5
source share
2 answers

Indicates the number of repetitions, the number of repetitions is used to determine the average value. For instance:

 %timeit -n 250 a = 2 # 61.9 ns ± 1.01 ns per loop (mean ± std. dev. of 7 runs, 250 loops each) %timeit -n 250 -r 2 a = 2 # 62.6 ns ± 0 ns per loop (mean ± std. dev. of 2 runs, 250 loops each) 

The number of executions will be n * r , but statistics are based on the number of repeats ( r ), but the number of "cycles" for each repetition is determined based on number ( n ).

Basically you need a sufficiently large n , so the minimum number of cycles is accurate “enough” to represent the maximum possible runtime, but you also need a sufficiently large r to get accurate “statistics” on how reliable the measurement of the “maximum possible runtime” is (especially if you suspect some caching might happen).

For shallow timings you should always use r 3 , 5 or 7 (in most cases large enough) and choose n as high as possible - but not too high, you probably want it to finish in a reasonable amount of time :-)

+4
source
 timeit -n 250 <statement> 

The statement will be executed 3 * 250 = 750 times ( -r has a default value of 3)

 timeit -n 250 -r 4 <statement> 

The application will be executed 4 * 250 = 1000 times

-r - how many times to repeat the timer (in the above examples, every time the timer is called with -n 250, which means 250 executions)

+1
source

Source: https://habr.com/ru/post/1271512/


All Articles