Jupyter Snippet CB2nd 01_timeit
Jupyter Snippet CB2nd 01_timeit
4.1. Evaluating the time taken by a command in IPython
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 s = 0.
for i in range(1, n):
s += 1. / i**2
22 ms ± 522 µs per loop (mean ± std. dev. of 7 runs,
10 loops each)
import numpy as np
%timeit np.sum(1. / np.arange(1., n) ** 2)
160 µs ± 959 ns per loop (mean ± std. dev. of 7 runs,
10000 loops each)