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

Install

Linux:

Use pip to install this package:

pip install line_profiler

Windows

It is easier to install using conda, since it does not require building the package from source:

# 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. ↩︎