ipython The %timeit and `%time` magics


The %timeit magic runs the given code many times, then returns the speed of the fastest result.

In [1]: %timeit sum(range(100000))
100 loops, best of 3: 2.91 ms per loop

The %%timeit cell magic can be used to time blocks of code.

In [2]: %%timeit
   ...: a = 0
   ...: for i in range(100000):
   ...:     a += i
100 loops, best of 3: 9.67 ms per loop

The %time magic times a single run of a function, similar to the Unix time command. Unlike %timeit, %time also shows the result.

In [3]: %time sum(range(100000))
CPU times: user 2.68 ms, sys: 3 µs, total: 2.68 ms
Wall time: 2.69 ms
Out[3]: 4999950000