[python每日一库]——hotshot

High performance logging profiler

官方文档:http://docs.python.org/2/library/hotshot.html#module-hotshot 

在 2.x 中,hotshot 用来做性能统计,开销比 profile/cProfile 更小。但 3.x 开始, hotshot 就不大行了。

 
只需要启动 hotshot.Profile(),并执行要统计的代码段。
代码段:
 1. test.py 
# test.py
import hotshot

def printMe():
    print "Hello"

p = hotshot.Profile("test.prof")
p.start()

printMe()

p.close()

2. proftest.py

# proftest.py
import hotshot.stats

stats = hotshot.stats.load("test.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats(20)

3.运行截图:

当执行了test.py 以后,会在文件里自动生成test.prof文件,以供proftest.py 读取。
posted @ 2013-08-30 16:48  小侠女  阅读(634)  评论(0编辑  收藏  举报