Thursday, 2 January 2020

-n and -r arguments to IPython's %timeit magic

I would like to time a code block using the timeit magic command in a Jupyter notebook. According to the documentation, timeit takes several arguments. Two in particular control number of loops and number of repetitions. What isn't clear to me is the distinction between these two arguments. For example

import numpy
N = 1000000
v = numpy.arange(N)

%timeit -n 10 -r 500 pass; w = v + v

will run 10 loops and 500 repetitions. My question is,

Can this be interpreted as the following? (with obvious differences the actual timing results)

import time
n = 10
r = 500
T = numpy.empty(r)
for j in range(r):
    t0 = time.time()
    for i in range(n):
        w = v + v
    T[j] = (time.time() - t0)/n

print('Best time is {:.4f} ms'.format(max(T)*1000))

An assumption I am making, and may well be incorrect, is that the time for the inner loop is averaged over the n iterations through this loop. Then the best of 500 repetitions of this loop is taken.

I have searched the documentation, and haven't found anything that specifies exactly what this is doing. For example, the documentation here is

Options: -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

Nothing is really said about how the inner loop is timed. The final results is the "best" of what?

The code I want to time does not involve any randomness, so I am wondering if I should set this inner loop to n=1. Then, the r repetitions will take care of any system variability.



from -n and -r arguments to IPython's %timeit magic

No comments:

Post a Comment