January 2018
Beginner to intermediate
548 pages
12h 11m
English
The %timeit magic and the %%timeit cell magic (which applies to an entire code cell) allow us to quickly evaluate the time taken by one or several Python statements. The next recipes in this chapter will show methods for more extensive profiling.
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:>>> n = 100000
>>> %timeit sum([1. / i**2 for i in range(1, n)])
21.6 ms ± 343 µs per loop (mean ± std. dev. of 7 runs,
10 loops each)%%timeit cell magic to time the same computation written on two lines:>>> %%timeit s = 0. for i in range(1, ...
Read now
Unlock full access