我想分析 python 代码的性能,为此我使用了 cProfile 模块并生成了 .cprof 文件,如 python documentation 中所述.我正在使用 pyprof2calltree python 模块将 .cprof 文件打开到 KCacheGrind 中。 .我放了分析结果的截图,它显示名为 cycle 5 的函数占用了 100.04% 的 CPU 时间。我无法理解这代表什么。它也没有显示此功能的任何源代码。
最佳答案
it shows that function named cycle 5 is taking 100.04% of the CPU time.
不,它表明某些“循环 5”和从它调用的所有函数以及从它们调用的所有函数都使用 100%“包含”时间。
<cycle>
不是真正的功能,它是如何 kcachegrind heuristically tries to get recursion information from the profiling format (“周期内调用的包含成本是没有意义的”)。 This format (defined for callgrind)没有函数调用序列的确切信息(f1 调用 f2,调用 f3 ...),仅存储调用者-被调用者对。此格式仅对“Self”时间准确,但在递归时对“Inclusive”(包括所有被调用者时间)不准确。
KCachegrind 允许(并推荐)您使用 View Menu 关闭“Do Cycle Detection” : https://kcachegrind.github.io/html/NewsOld.html
Cycle detection is switchable with a toolbar button. For GUI application, sometimes it's useful to switch cycle detection off, even if there are some visualization errors with recursive calls.
没有循环检测没有合成 <cycle>
将生成函数,但某些函数可能具有 >100%“包含”。时间。尝试使用“ self ”时间,或具有更好格式的分析工具(linux perf
、operf
、ocperf.py
;google 的 cpuprofile 和其他使用具有完整函数调用堆栈的分析格式)。 https://github.com/jrfonseca/gprof2dot列出了许多好的格式,它也可以正确地可视化它们(如果有足够的信息)。尝试使用 python 配置文件格式:
python profile
python -m profile -o output.pstats path/to/your/script arg1 arg2 gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png
python cProfile (formerly known as lsprof)
python -m cProfile -o output.pstats path/to/your/script arg1 arg2 gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png
python hotshot profiler
The hotshot profiler does not include a main function. Use the hotshotmain.py script instead.
hotshotmain.py -o output.pstats path/to/your/script arg1 arg2 gprof2dot.py -f pstats output.pstats | dot -Tpng -o output.png
关于python - 使用 KchacheGrind 分析的 cProfile 输出中的 <cycle 5> 函数是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44430353/