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.py
Function: do_stuff at line 193

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   193                                           def do_stuff(numbers):
   194         1         21.0     21.0     19.4      s = sum(numbers)
   195         1         45.0     45.0     41.7      l = [numbers[i] / 43 for i in range(len(numbers))]
   196         1         42.0     42.0     38.9      m = ['hello' + str(numbers[i]) for i in range(len(numbers))]

多函数调用

当你需要调用不止一个函数的时候,就可以使用add_function函数去添加额外需要监控的函数

from line_profiler import LineProfiler

def second_function():
    # just for test
    i = 5
    pass

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))]
    for i in range(5):
        second_function()


if __name__ == '__main__':
    number = [1,2,3,4,5,6]
    p = LineProfiler()
    p.add_function(second_function)
    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: 2.4e-06 s
File: C:/Users/admin/Desktop/GPflowMPC_cui _contract - profile/temp.py
Function: second_function at line 193

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   193                                           def second_function():
   194                                               # just for test
   195         5         14.0      2.8     58.3      i = 5
   196         5         10.0      2.0     41.7      pass

Total time: 2.44e-05 s
File: C:/Users/admin/Desktop/GPflowMPC_cui _contract - profile/temp.py
Function: do_stuff at line 198

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
   198                                           def do_stuff(numbers):
   199         1         22.0     22.0      9.0      s = sum(numbers)
   200         1         48.0     48.0     19.7      l = [numbers[i] / 43 for i in range(len(numbers))]
   201         1         45.0     45.0     18.4      m = ['hello' + str(numbers[i]) for i in range(len(numbers))]
   202         6         32.0      5.3     13.1      for i in range(5):
   203         5         97.0     19.4     39.8          second_function()

方法2:使用装饰器@profile

在需要检测的函数上面添加@profile装饰符号, (此时调包是调用profile 而不是line_profiler)。
在命令行中使用kernprof -l -v test.py启动,结束会在窗口打印逐行信息以及生成一个lprof文件。 与方法1 相似。
问题在于每次不在使用profile 查看性能时,需要将函数上的装饰类注释掉

读取lprof 文件

进入当前目录后,在命令行中使用
python -m line_profiler saveName.lprof (saveName.lprof是我自己的文件名)

[1]添加python——profile、line_profiler和memory_profiler模块接描述
[2]【Python line_profiler & memory_profiler】分析每一行代码的耗时及内存占用情况
[3]python性能分析之 profile 模块 记录

[4]python 性能调试工具(line_profiler)使用

  • 8
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
PyCharm中安装line_profiler,可以按照以下步骤进行操作: 1. 打开PyCharm并进入你的项目。 2. 点击顶部菜单栏中的"File"。 3. 选择"Settings"。 4. 在设置窗口中,展开"Project"选项。 5. 选择"Project Interpreter"。 6. 在右侧的依赖项列表中,点击"+"按钮。 7. 在搜索框中输入"line_profiler"。 8. 在搜索结果中选择"line_profiler"并点击"Install Package"按钮。 9. 等待安装完成。 这样,你就成功在PyCharm中安装了line_profiler。现在,你可以在代码中使用line_profiler来进行性能分析了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Python性能分析工具Line_profiler](https://blog.csdn.net/weixin_44613728/article/details/120411325)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [python性能分析line_profiler模块-耗时,效率 时间](https://blog.csdn.net/kyle1314608/article/details/103123187)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值