python性能分析工具模块_Python - line_profiler性能分析工具

Python line_profiler 性能分析库,相比于 cProfile,输出更为直观.

line_profiler使用装饰器(@profile)标记需要调试的函数. 用 kernprof.py 脚本运行代码, 被选函数每一行花费的 cpu 时间以及其他信息就会被记录下来.

1. 安装pip install Cpython

pip install Cython git+https://github.com/rkern/line_profiler.git

#或

pip install line-profiler

2. 使用

在需要评估性能的函数前加上 @profile,形式如:@profile

def fun():

pass

@profile

def main():

pass

if __name__ == '__main__':

main()

其中,@profile 只是一个标记,不是 Python 的语句,所以会导致代码不能直接运行,只能用专门的方法运行:kernprof -l -v xxxxxx.py

-l 表示逐行分析, -v 用于输出。同时会输出一个后缀为.py.lprof 的文件,后期可以对.lprof文件进行分析.

注:@profile 用法有一点限制,不可以用于 class ,但是可以用于class 的方法上;子函数也可以用;并且可以同时 profile 多个函数.

3. 示例

loopdemo.py:@profile

def foo():

task = []

for a in range(0, 101):

for b in range(0, 101):

if a + b == 100:

task.append((a, b))

return task

@profile

def run():

for item in foo():

pass

if __name__ == '__main__':

run()

运行:kernprof -l -v loopdemo.py

输出结果形式如:Wrote profile results to loopdemo.py.lprof

Timer unit: 1e-06 s

Total time: 0.009856 s

File: loopdemo.py

Function: foo at line 1

Line # Hits Time Per Hit % Time Line Contents

==============================================================

1 @profile

2 def foo():

3 1 1.0 1.0 0.0 task = []

4

5 102 47.0 0.5 0.5 for a in range(0, 101):

6 10302 4741.0 0.5 48.1 for b in range(0, 101):

7 10201 4975.0 0.5 50.5 if a + b == 100:

8 101 91.0 0.9 0.9 task.append((a, b))

9 1 1.0 1.0 0.0 return task

Total time: 0.017778 s

File: loopdemo.py

Function: run at line 12

Line # Hits Time Per Hit % Time Line Contents

==============================================================

12 @profile

13 def run():

14 102 17747.0 174.0 99.8 for item in foo():

15 101 31.0 0.3 0.2 pass

其中,

[1] - Total time,总耗时

[2] - Hits,调用次数

[3] - %Time 列,指出哪行代码占了它所在函数的消耗的时间百分比.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值