I’m profiling in Python using cProfile. I found a function that takes a lot of CPU time. How do I find out which function is calling this heavy function the most?
EDIT:
I’ll settle for a workaround: Can I write a Python line inside that heavy function that will print the name of the function that called it?
That may not answer your question directly, but will definitely help. If use the profiler with option –sort cumulative it will sort the functions by cumulative time. Which is helpful to detect not only heavy functions but the functions that call them.
There is a workaround to get the caller function:
You can add as many f_back as you want in case you want the caller caller etc
If you want to calculate frequent calls you can do this:
Then print them by order of frequency: