在将 cProfile 作为脚本调用时,我可以关闭部分 Python 代码中的分析吗?

问题描述 投票:0回答:1

我想分析我的代码,但排除启动。

Python 文档 描述 如何将 cProfile 作为脚本运行:

python -m cProfile [-o output_file] [-s sort_order] (-m module | myscript.py)

他们还描述了如何使用 python API 打开和关闭分析:

import cProfile

pr = cProfile.Profile()
pr.enable()
# ... do something ...
pr.disable()

如果我将 cProfile 作为模块运行,运行

pr.enable()
pr.disable()
会有效吗?

启动我的代码时是否有一个隐含的“启用”,我可以禁用它,或者脚本方法使用的

cProfile
对象对我来说是不可访问的?

python profiling cprofile
1个回答
0
投票

似乎无法访问作为脚本运行时创建的

cProfile.Profile
对象。下一个最好的办法是在程序中尽可能靠近开始处实例化
cProfile.Profile
,并在该实例上调用
disable()
enable()

© www.soinside.com 2019 - 2024. All rights reserved.