Week 3 Videos

Timing using %%timeit

Some common abbreviations:

  • ms means \(10^{-3}\) seconds

  • µs means \(10^{-6}\) seconds

  • ns means \(10^{-9}\) seconds

import numpy as np
my_array = np.arange(1,10**7)
type(my_array)
numpy.ndarray
my_list = list(my_array)
my_set = set(my_array)
-2 in my_array
False
-2 in my_list
False
-2 in my_set
False
%%timeit
-2 in my_array
3.06 ms ± 3.84 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
%%timeit
-2 in my_list
955 ms ± 2.31 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
-2 in my_set
29 ns ± 0.195 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
1/my_array[:10]
array([1.        , 0.5       , 0.33333333, 0.25      , 0.2       ,
       0.16666667, 0.14285714, 0.125     , 0.11111111, 0.1       ])
%%timeit
new_list = []
for x in my_list:
    new_list.append(1/x)
1.34 s ± 14.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
[1/x for x in my_list]
1.17 s ± 13.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
1/my_array
6.24 ms ± 21.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Created in deepnote.com Created in Deepnote