How can I do profiling by line in Python 3? The standard profilers have only function-level precision.
Asked
Active
Viewed 6,243 times
19
-
1If you have the profiles of all of the functions, doesn't that mean that you have all of the lines??? – cwallenpoole Jun 08 '11 at 00:52
-
1@cwallenpoole: No it does not. But if you had the profile of all the lines, you would probably have the profile of all the functions. – Matt Joiner Jun 08 '11 at 03:40
-
Hi Matt. FWIW, I'm pretty sure we've discussed [this](http://stackoverflow.com/questions/4387895/if-profiler-is-not-the-answer-what-other-choices-do-we-have/4390868#4390868) before. It gives you approximate cost and exact location of the heaviest lines, whether exclusive or inclusive. – Mike Dunlavey Jun 08 '11 at 04:53
-
1Have you tried, CProfile or hotshot? – Aug 02 '12 at 02:11
-
1@cwallenpoole: No. But if the lines of a function are disjoint from any other function's lines as they are in Python, then having the profile of all lines means you can calculate the profile all functions. I'm not aware of cprofile fitting this criterion. – Matt Joiner Aug 02 '12 at 05:08
3 Answers
12
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.

user673679
- 1,327
- 1
- 16
- 35
-
2Nice! You answered just in time for me to see it :) Any chance of getting your 3.x update into the "main" version? Or better yet, get line_profiler into the Python stdlib... – Dubslow Nov 06 '12 at 22:45
5
There's a nice Pull Request that implements Python 3 support very well

Filippo Valsorda
- 938
- 1
- 10
- 16
3
As for the line_profiler, a recent working pull request is here waiting for an inclusion. The fork can be installed via:
pip install cython
pip install -e hg+https://bitbucket.org/kmike/line_profiler@7999f21#egg=line_profiler
It works for me at least on Python 3.4.1 with IPython magic functions.

porglezomp
- 1,333
- 12
- 24

mike_k
- 1,831
- 2
- 14
- 12