needleinthehay.de

Profile Runtime Speed of Code

Using timeit to measure execution.

import timeit

def some_function():
    return sum((i for i in range(1, 2**24)))

run_time = timeit.timeit("some_function()",
                  setup="from __main__ import some_function",
                  number=10)
print(run_time)

# OUT:
# 6.82225431400002

In jupyter, use the magic to measure runtime of cell

%%time  # for time of single run
%%timeit # for time of iterative runs

some_function()

Use cProfiler with snakeviz to get a nice Chart in Jupyter Lab:

# install
pip install snakeviz
# Add at beginning of Notebook
%load_ext snakeviz

# Add to beginng of cell to measure:
%%snakeviz