September 2014
Intermediate to advanced
512 pages
12h 39m
English
The %timeit magic and the %%timeit cell magic (that applies to an entire code cell) allow you to quickly evaluate the time taken by one or several Python statements. For more extensive profiling, you may need to use more advanced methods presented in the next recipes.
We are going to estimate the time taken to calculate the sum of the inverse squares of all positive integer numbers up to a given n:
n:In [1]: n = 100000
In [2]: %timeit sum([1. / i**2 for i in range(1, n)]) 10 loops, best of 3: 131 ms per loop
%%timeit cell magic to time the same computation written on two lines:In [3]: %%timeit s = 0. for i in ...
Read now
Unlock full access