perfy-一个针对python的轻量级性能跟踪程序
perf的Python项目详细描述
性能
perfy-一个针对python的轻量级分析工具
将函数和方法标记为使用@perfydecorator启用的跟踪:
@perfydeftrace_this_function():# do something
此外,还可以使用和语句跟踪代码的任意块:
withperfy('name of your choice'):# your code here
运行代码后,使用
perfy.report()
示例:
fromtimeimportsleepfromperfyimportperfy@perfy# <-- use decorator to track function callsdeffunc_sleep():sleep(.02)defsleep_loop():withperfy('sleep loop'):# <-- use with-statement to track arbitrary block of codefor_inrange(10):func_sleep()# you can nest with-blocks and decorated function calls in any order:@perfy# <-- a decorator on a top level functiondefmain():sleep_loop()# <-- this functions has a traced block insidewithperfy('custom named block'):# <-- traced blocksleep(.1)withperfy('inner block'):# <-- nested traced blockfunc_sleep()func_sleep()
对于以上代码perfy.report()将输出:
----------------------------------Perfy report----------------------------------
Function/Method Time(sec.) Calls(count)
--------------------------------------------------------------------------------
main 0.350 1
└ sleep loop 0.206 1
└ func_sleep 0.205 10
└ custom named block 0.144 1
└ inner block 0.043 1
└ func_sleep 0.042 2
--------------------------------------------------------------------------------