To profile Python code line by line. We can use line_profiler1.

Install

Linux:

Just use pip:

pip install line_profiler

Windows

It is easier to use conda since it does not require building the package:

# if you use pip on Windows, you must have Visual Studio to build the package.
conda install line_profiler

How to profile?

Annotate the function we want to profile with @profile annotation. On the command line, run:

kernprof -v -l test_script.py

The command line options to test_script.py can be followed after it. Just like you call python test_script.py.

Ref


  1. This is a fork of original line_profiler repo. The original repo is not maintained. ↩︎