Time the performance by timeit and cell magic

Goal

This post aims to introduce how to measure the running time of your code or function by using timeit module and cell magic command %%time and %%timeit.

Reference *Time a Python Function

Libraries

In [1]:
import pandas as pd
import timeit

Measure a literal code

In [2]:
timeit.timeit('2**10')
Out[2]:
0.01853936500265263

Measure the execution time for a function

In [4]:
def execute_abc(x=10):
    return 2 ** 10
In [7]:
timeit.timeit(execute_abc)
Out[7]:
0.0984989230055362

Measure the time by using Cell Magic in Jupyter Notebook

In [3]:
%%time
2**10
CPU times: user 4 µs, sys: 1e+03 ns, total: 5 µs
Wall time: 11 µs
Out[3]:
1024
In [9]:
%timeit 2**10
17.9 ns ± 0.597 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

Comments

Comments powered by Disqus