Skip to content

Instantly share code, notes, and snippets.

@ctokheim
Created November 11, 2014 18:54
Show Gist options
  • Select an option

  • Save ctokheim/9fb75b91667f824db6f3 to your computer and use it in GitHub Desktop.

Select an option

Save ctokheim/9fb75b91667f824db6f3 to your computer and use it in GitHub Desktop.
Profiling Python Code

Profiling Python Code

Line-by-line profiling

Many times a line-by-line timing description is necessary to understand where slow downs in the code exist. Just using %timeit in python can give you an understanding of how long functions take but not inidividual lines. There is a line-by-line profiler that has been developed on github here.

Install by:

$ pip install line_profiler

Know you should have a script called kernprof.py in your python's bin directory which will perform the profiling. Now add the @profile decorator to the function you wish to profile line-by-line:

@profile
def main(opts):
    # do some work in function
    # line 1
    # line 2 ...
    pass

Then evaluate performance:

$ python /path/to/kernprof.py -l -v myscript.py 

-v forces showing timing statistics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment