On this page
26.5. 热点—高性能日志记录探查器
2.2 版中的新Function。
该模块为_hotshot
C 模块提供了更好的接口。 Hotshot 替代了现有的profile模块。由于它主要是用 C 编写的,因此与现有的profile模块相比,它对性能的影响应小得多。
在版本 2.5 中进行了更改:结果应该比过去更有意义:计时核心包含一个严重的错误。
Note
hotshot分析器尚不适用于线程。如果可能的话,使用非线程脚本在您想要测量的代码上运行探查器非常有用。
-
- class *
hotshot.
Profile
(* logfile * [,* lineevents * [,* linetimings *]])
- 探查器对象。参数* logfile 是用于记录的概要文件数据的日志文件的名称。参数 lineevents 指定是为每个源代码行生成事件,还是仅在函数调用/返回时生成事件。默认为
0
(仅日志函数调用/返回)。参数 linetimings *指定是否记录定时信息。默认为1
(存储计时信息)。
- class *
26.5.1. 配置文件对象
概要文件对象具有以下方法:
Profile.
addinfo
(* key , value *)- 将任意带标签的值添加到概要文件输出。
Profile.
close
( )- 关闭日志文件并终止分析器。
Profile.
fileno
( )- 返回分析器的日志文件的文件 Descriptors。
Profile.
run
(* cmd *)Profile.
runcall
(* func *, *args , 关键字)- 配置一个可调用对象的单个呼叫。可能会传递其他位置和关键字参数。返回调用的结果,并允许异常清晰地传播,同时确保在出局时禁用概要分析。
Profile.
runctx
(* cmd , globals , locals *)- 在特定环境中评估exec兼容的字符串。在开始分析之前,将对字符串进行编译。
Profile.
start
( )- 启动事件探查器。
Profile.
stop
( )- 停止探查器。
26.5.2. 使用热门数据
2.2 版中的新Function。
该模块将热点分析数据加载到标准的pstats Stats 对象中。
hotshot.stats.
load
(* filename *)- 从* filename *加载热点数据。返回pstats.Stats类的实例。
26.5.3. 用法示例
请注意,此示例运行 Python“基准”分析。运行可能需要一些时间,并且会产生较大的输出文件。
>>> import hotshot, hotshot.stats, test.pystone
>>> prof = hotshot.Profile("stones.prof")
>>> benchtime, stones = prof.runcall(test.pystone.pystones)
>>> prof.close()
>>> stats = hotshot.stats.load("stones.prof")
>>> stats.strip_dirs()
>>> stats.sort_stats('time', 'calls')
>>> stats.print_stats(20)
850004 function calls in 10.090 CPU seconds
Ordered by: internal time, call count
ncalls tottime percall cumtime percall filename:lineno(function)
1 3.295 3.295 10.090 10.090 pystone.py:79(Proc0)
150000 1.315 0.000 1.315 0.000 pystone.py:203(Proc7)
50000 1.313 0.000 1.463 0.000 pystone.py:229(Func2)
.
.
.