Search code examples
pythonnumpyworkflowprofile

Python profiling: time spent on each line of function


I have been studying examples from the profile documentation and I have come to the workflow when I run

import cProfile as profile
import pstats

pr = profile.Profile()
pr.runcall(myFunc, args, kwargs)

st = pstats.Stats(pr)
st.print_stats() # and other methods like print_callees, print_callers, etc.

This gives me some general stats of the number of calls made and so on. Mind you, it is rather cryptic: I heaviliy use numpy inside myFunc (like numpy.sum, * and the like) but I cannot find those calls in the stats object. What I would like to see is the time spent on each line of the source code of function myFunc. How do I do that?


Solution

  • There is a good package for that on PyPI - line_profiler