Ben Chuanlong Du's Blog

It is never too late to learn.

Python Profiler for JupyterLab Notebooks

%time

Measue the execuation time of the code ONCE.

%timeit

Measure the execuation time of the code (accurately) by running it MULTIPLE TIMES and taking the average.

%prun | %%prun

-D: output the profiling results into a file so that you can other tools (e.g., snakeviz) to visualize it.

Noticd that %prun and %%prun are based on cProfile or profile, which are designed to provide an execution profile for a given program, not for benchmarking purposes (for that, there is time and timeit for reasonably accurate results). This particularly applies to benchmarking Python code against C code: the profilers introduce overhead for Python code, but not for C-level functions, and so the C code would seem faster than any Python one. Overall, %prun and %%prun might slow down your Python code up to 3 times.

%%snakeviz

In [ ]:
pip3 install snakeviz
In [ ]:
%load_ext snakeviz

pyheatmagic

Installation

pip3 install py-heat-magic

Usage

Load the magic.

%load_ext heat

%%heat

%%heat -o file.png

Line Profiler

Within your jupyter notebook, call: %load_ext line_profiler

In [ ]:
 

Comments