相当于Linux命令,用于在Windows平台上使用graphviz的pstats和dot.exe运行python profiler

2024-10-01 09:33:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要运行一个python分析器工具,并对其进行可视化。 为此,使用以下简单代码:

import cProfile, pstats, io
from pstats import SortKey
pr = cProfile.Profile()
pr.enable()
s=0
for i in range(10):
    s=s+i
print(s)
pr.disable()
s = io.StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print(s.getvalue())

代码的输出如下所示:

45

在0.000秒内进行2次函数调用

排序人:累计时间

ncalls tottime percall cumtime percall文件名:lineno(函数)

1 0.000 0.000 0.000 0.000{内置方法内置.print}

1 0.000 0.000 0.000 0.000{方法'disable'的''lsprof.Profiler'对象}

在Linux中,可以使用以下命令将其保存为.pstats文件: python3-m cProfile-o test.pstats pro1.py 在此之后,可以使用Linux命令获得显示配置文件的图像文件:

gprof2dot-f pstats test.pstats | dot-Tpng-o output.png

请注意,必须在设置中安装graphviz和gprof2dot

现在的问题是,在第一步之后,我无法在Windows10中执行接下来的两个步骤

已定义的集合如中所述:

https://pythonfiles.wordpress.com/2017/06/01/hunting-performance-in-python-code-part-3/

我以前在Protege(本体编辑器)中使用过graphviz

有人能帮我吗

最后,如果这个代码不能在Windows中完成,请让我知道


Tags: 代码inioimportstatspr内置cprofile