Python性能分析工具Line_profiler

介绍

profile和line_profiler两个模块都是性能分析工具。有时候需要找到代码中运行速度较慢处或瓶颈,可以通过这两模块实现,而不再使用time计时。
line_profiler模块可以记录每行代码的运行时间和耗时百分比。1

安装

我是使用的anaconda安装的,所以直接pip install line_profiler即可。

使用

方法1 :生成一个Line_profiler类(推荐)

简单版本

from line_profiler import LineProfiler

def do_stuff(numbers):
    s = sum(numbers)
    l = [numbers[i] / 43 for i in range(len(numbers))]
    m = ['hello' + str(numbers[i]) for i in range(len(numbers))]


if __name__ == '__main__':
    number = [1,2,3,4,5,6]
    p = LineProfiler()
    p_wrap = p(do_stuff)
    p_wrap(number)
    p.print_stats()    # 控制台打印相关信息
    p.dump_stats('saveName.lprof')   # 当前项目根目录下保存文件

输出结果:

"D:\Program Files\Anaconda3\envs\tensorflow2.3\python.exe" "C:/Users/admin/Desktop/xxxx/temp.py"
Timer unit: 1e-07 s

Total time: 1.08e-05 s
File: C:/Users/admin/Desktop/GPflowMPC_cui _contract - profile/temp
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值