Python profiling: time spent on each function line

I was studying examples from the profile documentation , and I came to a workflow when I started

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 general statistics on the number of calls made, etc. Keep in mind that it is quite mysterious: I use the numpyinside myFunc(for example numpy.sum, *and the like) numpy), but I can not find these calls in the object statistics. What I would like to see is the time spent on each line of the source code of the function myFunc. How to do it?

+4
source share
1 answer

There is a nice package for PyPI - line_profiler

+3

Source: https://habr.com/ru/post/1567985/


All Articles