Profiling in Python: Who called the function?

后端 未结 8 1823
执念已碎
执念已碎 2020-12-22 15:17

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 mo

8条回答
  •  时光取名叫无心
    2020-12-22 15:55

    I almost always view the output of the cProfile module using Gprof2dot, basically it converts the output into a graphvis graph (a .dot file), for example:

    It makes it very easy to determine which function is slowest, and which function[s] called it.

    Usage is:

    python -m cProfile -o output.pstats path/to/your/script arg1 arg2
    gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png
    

提交回复
热议问题