Search code examples
pythonipythonprofilerline-profiler

How can I profile only my code in Python or IPython?


I'm familiar with the %prun magic command in IPython that uses the Python profile module. However, I'd like to only profile my code. That is, I'd like to see which are the slowest lines in my Python code, not the lines buried deep in some external package I'm using that get called often and therefore appear to take the most time. How can I do this?


Solution

  • Based on a little more Googling it seems that line_profiler is the answer: https://github.com/rkern/line_profiler

    import line_profiler
    %load_ext line_profiler
    

    Then just

    %lprun -f function_i_want_to_profile function_i_want_to_run()