/
timeit_module.py
38 lines (28 loc) · 1.16 KB
/
timeit_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import timeit
def test(n):
return sum(range(n))
n = 10000
loop = 1000
result = timeit.timeit('test(n)', globals=globals(), number=loop)
print(result / loop)
# 0.0002666301020071842
result = timeit.timeit(lambda: test(n), number=loop)
print(result / loop)
# 0.00027574066299712287
print(timeit.timeit(lambda: test(n), number=1))
print(timeit.timeit(lambda: test(n), number=10))
print(timeit.timeit(lambda: test(n), number=100))
# 0.0003999490290880203
# 0.0038685189792886376
# 0.03517670702422038
repeat = 5
print(timeit.repeat(lambda: test(n), repeat=repeat, number=100))
# [0.044914519996382296, 0.039663890027441084, 0.02868645201670006, 0.022745631984435022, 0.023260265996214002]
# magic command
# It works only on Jupyter / IPython.
get_ipython().magic('timeit test(n)')
# 259 µs ± 4.87 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
get_ipython().magic('timeit -r 3 -n 10000 test(n)')
# 237 µs ± 6.44 µs per loop (mean ± std. dev. of 3 runs, 10000 loops each)
get_ipython().run_cell_magic('timeit', '-r 3 -n 10000', 'import numpy as np\na = np.arange(n)\nnp.sum(a)')
# 19.7 µs ± 9.57 µs per loop (mean ± std. dev. of 3 runs, 10000 loops each)