line_profiler is a pip package that can be used to profile python code, and get a result with line-by-line execution details.
Usage:
-
Create your python file with an annotated function that you’d like to profile
-
Run the file with kernprof:
This will generate an lprof
file that can be analyzed with line_profiler:
-
Obtain the metrics:
Here’s an example without wrapping:
It can even be run in Jupyter notebooks with the use of the %line_profiler
extension (source):