O'Reilly logo

Python High Performance Programming by Gabriele Lanaro

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Profile line by line with line_profiler

Now that we know which function we have to optimize, we can use the line_profiler module that shows us how time is spent in a line-by-line fashion. This is very useful in situations where it's difficult to determine which statements are costly. The line_profiler module is a third-party module that is available on the Python Package Index and can be installed by following the instructions on its website:

http://pythonhosted.org/line_profiler/

In order to use line_profiler, we need to apply a @profile decorator to the functions we intend to monitor. Notice that you don't have to import the profile function from another module, as it gets injected in the global namespace when running the profiling script

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required