Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Profiling by line with Python 3

How can I do profiling by line in Python 3? The standard profilers have only function-level precision.

like image 257
Matt Joiner Avatar asked Jun 08 '11 00:06

Matt Joiner


People also ask

How do you use line profiler in Python?

The line_profiler test cases (found on GitHub) have an example of how to generate profile data from within a Python script. You have to wrap the function that you want to profile and then call the wrapper passing any desired function arguments. Also, you can add additional functions to be profiled as well.

What is line profiler in Python?

line_profiler is a module for doing line-by-line profiling of functions. kernprof is a convenient script for running either line_profiler or the Python standard library's cProfile or profile modules, depending on what is available. They are available under a BSD license.

Which tool will Analyse the data collected by the Python profiler?

Python Profilers, like cProfile helps to find which part of the program or code takes more time to run. This article will walk you through the process of using cProfile module for extracting profiling data, using the pstats module to report it and snakeviz for visualization.


2 Answers

While line_profiler only works for Python 2.x, it doesn't seem too hard to make the necessary changes to get it to work with 3.x.

I've done this myself here. It's quick and dirty and pretty much untested, so use it at your peril, but it's possibly a start.

like image 98
user673679 Avatar answered Jan 10 '23 15:01

user673679


There's a nice Pull Request that implements Python 3 support very well

https://bitbucket.org/robertkern/line_profiler/pull-request/2/python-25-33-compatibility-using-a-single/

like image 26
FiloSottile Avatar answered Jan 10 '23 13:01

FiloSottile