Python's profilehooks are really a great way to identify performance bottlenecks. I am using the @profile
decorator and usually I have an output similar to this:
ncalls tottime percall cumtime percall filename:lineno(function)
1 14.408 14.408 115.486 115.486 ...
1 0.000 0.000 75.578 75.578 ...
2 0.000 0.000 75.578 37.789 ...
2 74.937 37.469 75.578 37.789 ...
10 0.000 0.000 23.893 2.389 ...
10 0.001 0.000 23.893 2.389 ...
10 0.461 0.046 23.891 2.389 ...
35969 1.663 0.000 23.440 0.001 ...
107907 4.825 0.000 18.174 0.000 ...
252099 5.917 0.000 11.686 0.000 threadsafe.py:42(__call__)
4 0.000 0.000 11.379 2.845 ...
4 0.000 0.000 11.366 2.842 ...
107907 1.203 0.000 8.441 0.000 ...
It seems as threadsafe.py:42 is unnecessarily called very often by some code of mine. Now I need to know why it is called that often. In the past I used to set a breakpoint in this function to identify who is calling this method. It turns out to be very time-consuming, because many of those calls are are valid.
Can you share a better way to identify which code is calling another function in python? Maybe a kind of result list ordered by frequency?
Thank you!