我正在尝试分析调用其他函数的函数.我将分析器称为如下:
from mymodule import foo
def start():
# ...
foo()
import cProfile as profile
profile.run('start()', output_file)
p = pstats.Stats(output_file)
print "name: "
print p.sort_stats('name')
print "all stats: "
p.print_stats()
print "cumulative (top 10): "
p.sort_stats('cumulative').print_stats(10)
Run Code Online (Sandbox Code Playgroud)
我发现分析器说所有的时间都花在mymodule的函数"foo()"上,而不是把它放到子函数foo()调用中,这就是我想要看到的.如何让分析器报告这些功能的性能?
谢谢.
Iva*_*hko -1
也许您遇到了类似的问题,所以我将在这里描述我的问题。我的分析代码如下所示:
def foobar():
import cProfile
pr = cProfile.Profile()
pr.enable()
for event in reader.events():
baz()
# and other things
pr.disable()
pr.dump_stats('result.prof')
Run Code Online (Sandbox Code Playgroud)
最终的分析输出仅包含events()调用。我花了不少时间才意识到我的循环分析是空的。foobar()当然,来自客户端代码的调用不止一次,但有意义的分析结果已被最后一次使用空循环的调用覆盖。