line_profiler增强,运行脚本无需删除@profile装饰器
来源:互联网 发布:漯河淘宝加盟被骗了 编辑:程序博客网 时间:2024/06/05 20:02
什么是line_profiler
用惯了MATLAB的人转移到Python上,优化程序的第一步应该就是找profile。Python的确内置profiler,有两种:profile和cProfile,一个是Python写的,另一个是用C写的扩展。自带的profiler输出结果较为完善,各个内建函数也都包含在内,但对于不那么熟悉Python内建函数的新手来讲,通过自带profiler去优化一段程序很难完成。
有一些现成的第三方包给内置profiler的输出文件做了扩展,可将其调用过程展示在GUI里,但无论如何还是没有每行代码单独统计时间来的方便。就算知道某个内建函数费时较多,但为了优化程序,还需找出调用此内建函数的语句,所以很有可能出现一个你的函数调用了一个内建函数,而它又调用了另一个内建函数,这就使得整个程序优化过程十分繁琐。
line_profiler很好的解决了这个问题,其输出结果就如同MATLAB中profile的输出结果,按照每行代码统计耗时以及调用次数。可从github上下载,其页面上有很详细的使用说明,这里就不介绍其使用方法。下载页面:
https://github.com/rkern/line_profiler
line_profiler增强
学会使用line_profiler后,发现其优化方式方便是方便,但对于函数很多的大段程序,或者对于包含很多方法的自建类的优化就没那么方便了。因为要使用line_profiler,需要再你想要time的函数前一行增加@profile
装饰器,例如:
# 不使用line_profilerdef example1(): print('This is an example without profiling')# 使用line_profiler@profiledef example2(): print('This is an example with profiling')
但是在加上@profile
装饰器后,程序无法直接运行,要想在类似spyder这种类MATLAB的IDE中边优化、边调试就很困难。优化之前要在函数前加上@profile
,调试时又要将@profile
删掉,整个过程很麻烦。无意间在书中看到既可以不删除@profile
,又可以正常调试程序的方法。方法出自:
High Performance Python by Micha Gorelick and Ian Ozsvald
将以下代码加入脚本最前面,如果是类的话,加在类的定义之前。
if '__builtin__' not in dir() or not hasattr(__builtin__, 'profile'): def profile(func): def inner(*args, **kwargs): return func(*args, **kwargs) return inner
书中给的代码仅适用于python2,因为python2和python3对于__builtin__的定义有些变化。适用于python3的代码如下,放在脚本最前面即可使用。
if 'builtins' not in dir() or not hasattr(builtins, 'profile'): import builtins def profile(func): def inner(*args, **kwargs): return func(*args, **kwargs) return inner builtins.__dict__['profile'] = profile
该方法的原理是在不使用line_profiler时重新定义一个@profile
装饰器,装饰器的作用是运行被装饰函数,除此之外没有其他作用,最后将其加入到环境变量的函数列表中即可。
- line_profiler增强,运行脚本无需删除@profile装饰器
- python 调试 函数运行时间line_profiler
- C++ 调用python脚本,无需解释器
- 删除profile
- Mininet安装增强包 实现共享文件夹 运行python脚本
- 使用装饰器模式做类的增强
- TCPIP PROFILE<参数配置文件,无需单独阅读>
- Python的7种性能测试工具:timeit、profile、cProfile、line_profiler、memory_profiler、PyCharm图形化性能测试工具、objgraph
- 使用memory_profiler 与 cProfile ,line_profiler 优化Python程序运行时间和内存消耗实例
- python装饰器计算函数运行时间
- Xcode5 删除provisioning profile
- 删除provisioning profile
- Xcode 删除provisioning profile
- 删除 Provisioning Profile
- 删除 Provisioning Profile
- Xcode删除provisioning profile
- xcode删除profile文件
- Provisioning Profile删除
- hdoj-1878-欧拉回路
- 反编译之apk1
- Android高手进阶:Adapter深入理解与优化
- 83. Remove Duplicates from Sorted List
- 视觉模板匹配代码
- line_profiler增强,运行脚本无需删除@profile装饰器
- Leetcode刷题记——7. Reverse Integer
- java设计模式之门面模式
- 74. Search a 2D Matrix
- 【甲は乙を···(さ)せます】使役形式
- matlab中图像直方图及其均衡化(含直方图)的实现
- eclipse +PyDev 设置
- c++学习之变量的存储类别
- 使用cocoaPods,pch提示找不到文件或者导入文件不智能索引