I have tried using the line_profiler module for getting a line-by-line profile over a Python file. This is what I've done so far:
1) Installed line_profiler from pypi by using the .exe file (I am on WinXP and Win7). Just clicked through the installation wizard.
2) Written a small piece of code (similar to what has been asked in another answered question here).
from line_profiler import LineProfiler
def do_stuff(numbers):
print numbers
numbers = 2
profile = LineProfiler(do_stuff(numbers))
profile.print_stats()
3) Run the code from IDLE/PyScripter. I got only the time.
Timer unit: 4.17188e-10 s
How do I get full line-by-line profile over the code I execute? I have never used any advanced Python features like decorators, so it is hard for me to understand how shall I use the guidelines provided by several posts like here and here.
Just follow Dan Riti's example from the first link, but use your code. All you have to do after installing the
line_profiler
module is add a@profile
decorator right before each function you wish to profile line-by-line and make sure each one is called at least once somewhere else in the code—so for your trivial example code that would be something like this:example.py
file:Having done that, run your script via the
kernprof.py
✶ that was installed in yourC:\Python27\Scripts
directory. Here's the (not very interesting) actual output from doing this in a Windows 7 command-line session:You likely need to adapt this last step—the running of your test script with
kernprof.py
instead of directly by the Python interpreter—in order to do the equivalent from within IDLE or PyScripter.✶Update
It appears that in
line_profiler
v1.0, thekernprof
utility is distributed as an executable, not a.py
script file as it was when I wrote the above. This means the following now needs to used to invoke it from the command-line: