Skip to main content
  1. Posts/

Profiling Python Code with line_profiler

··235 words·2 mins·
Python Profiling
Table of Contents

To profile Python code line by line, we can use line_profiler1.

How to install
#

Linux/macOS
#

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?
#

Line profiler has a command line execute named kernprof. Annotate the function we want to profile with @profile annotation. Then run the following command on the command line:

kernprof -v -l -u 1e-3 test_script.py

By default, kernprof will only generate the profile result file, but do not print the result. The name of result file is the original Python script name suffixed with lprof. To check the profile result, we need to run line_profiler again:

python -m line_profiler test_script.py.lprof

The -v option tells kernprof to show the profile result. The -l option will print the profile time for each line for the given function. The -u option specify the unit of measure, which is 1e-6 seconds by default.

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

References
#


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

Related

How to Profile Your Python Script/Module
·328 words·2 mins
Python Profiling
Retry for Google Cloud Client
·197 words·1 min
Python GCP
Make Python logging Work in GCP
·570 words·3 mins
Python Logging GCP