2024-05-23 18:11:59 发布
网友
我试图对一个相当复杂的Python程序执行一些高级评测。然而,当使用cProfile时,几乎所有时间都是以以下方式计量的:
{method 'enable' of '_lsProf.Profiler' objects}
如果我评测整个程序python -m cProfile ...,以及在Python代码中执行评测时(使用profile.enable()/.disable()/.create_stats...),就会发生这种情况
python -m cProfile ...
profile.enable()/.disable()/.create_stats...
我可能做错什么了吗?在
这是因为在你的代码中你有
import cProfile pr = cProfile.Profile() pr.enable()
这是为了手动保存结果或打印结果,如果您像您用python -m cProfile -o program.prof my_program.py所说的那样调用探查器,则不需要在程序中使用cProfile。在
python -m cProfile -o program.prof my_program.py
这看起来像是一个bug或限制。嵌套探查器不工作。这里有个建议:
import cProfile import pstats import time import cStringIO as StringIO def do(): for i in range(100): time.sleep(0.01) def do_more(): for i in range(100): time.sleep(0.01) def do_all(): all_profiler = cProfile.Profile() all_profiler.enable() profiler = cProfile.Profile() profiler.enable() do() profiler.disable() strio = StringIO.StringIO() stats = pstats.Stats(profiler, stream=strio) stats.sort_stats("cumulative") stats.print_stats(10) print "do profile:\n{}".format(strio.getvalue()) do_more() all_profiler.disable() strio = StringIO.StringIO() stats = pstats.Stats(all_profiler, stream=strio) stats.sort_stats("cumulative") stats.print_stats(10) print "all profile:\n{}".format(strio.getvalue()) do_all()
添加到临时股东大会在shell中运行,输出为:
请注意,“all_profiler”只显示另一个profiler。在
编辑:这可能只是摘要打印的问题。例如,我没有尝试将统计数据转储到配置文件查看器中,例如转换为使用kCachegrind。在
这是因为在你的代码中你有
这是为了手动保存结果或打印结果,如果您像您用
python -m cProfile -o program.prof my_program.py
所说的那样调用探查器,则不需要在程序中使用cProfile。在这看起来像是一个bug或限制。嵌套探查器不工作。这里有个建议:
添加到临时股东大会在shell中运行,输出为:
^{pr2}$请注意,“all_profiler”只显示另一个profiler。在
编辑:这可能只是摘要打印的问题。例如,我没有尝试将统计数据转储到配置文件查看器中,例如转换为使用kCachegrind。在
相关问题 更多 >
编程相关推荐