Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 894
alisianoi Avatar asked Oct 19 '22 20:10

alisianoi


1 Answers

There is a good package for that on PyPI - line_profiler

like image 86
Gill Bates Avatar answered Nov 03 '22 07:11

Gill Bates