https://blog.csdn.net/guofangxiandaihua/article/details/77825524
python性能调试过程中最突出的问题就是耗时,性能测试工具有很多,像profiler,cprofiler等等,都是只能返回函数整体的耗时,而line_profiler就能够很好解决这个问题(大家可以试试就知道了)。
怎么使用这个工具呢?网上大部分都是说在所需要测的函数前面加一个@profile,如文档所说。但是加了@profile后函数无法直接运行,只能优化的时候加上,调试的时候又得去掉。文章中提到了这个问题的解决办法,个人觉得还是有点麻烦,不太能理解这是为什么。我在stackoverflow上看到了另一种关于line_profile的使用方法,简单而且实用。
这是使用案例:
-
from line_profiler import LineProfiler
-
-
-
-
-
l = [numbers[i]/43 for i in range(len(numbers))]
-
m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
-
-
numbers = [random.randint(1,100) for i in range(1000)]
-
-
lp_wrapper = lp(do_stuff)
-
-
输出结果:
-
-
-
-
File: <ipython-input-2-2e060b054fea>
-
Function: do_stuff at line 4
-
-
-
==============================================================
-
-
5 1 10 10.0 1.5 s = sum(numbers)
-
6 1 186 186.0 28.7 l = [numbers[i]/43 for i in range(len(numbers))]
-
7 1 453 453.0 69.8 m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
这种方法就比较好用了,如果你想在其他文件中调用这个包含line_profiler函数测试的文件,又不想把这个信息显示出来,使用
if __name__ == '__main__':
把需要测试的函数放到这个下面,这样就能完美解决上述问题。
对于函数内部调用函数的情况:
-
from line_profiler import LineProfiler
-
-
-
def do_other_stuff(numbers):
-
-
-
-
-
l = [numbers[i]/43 for i in range(len(numbers))]
-
m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
-
-
numbers = [random.randint(1,100) for i in range(1000)]
-
-
lp_wrapper = lp(do_stuff)
-
-
这样做的话,只能显示子函数的总时间。输出如下:
-
-
-
-
File: <ipython-input-3-ec0394d0a501>
-
Function: do_stuff at line 7
-
-
-
==============================================================
-
-
8 1 11 11.0 1.4 do_other_stuff(numbers)
-
9 1 236 236.0 30.5 l = [numbers[i]/43 for i in range(len(numbers))]
-
10 1 526 526.0 68.0 m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
为了能够同时显示函数每行所用时间和调用函数每行所用时间,加入add_function就能够解决。
-
from line_profiler import LineProfiler
-
-
-
def do_other_stuff(numbers):
-
-
-
-
-
l = [numbers[i]/43 for i in range(len(numbers))]
-
m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
-
-
numbers = [random.randint(1,100) for i in range(1000)]
-
-
lp.add_function(do_other_stuff)
-
lp_wrapper = lp(do_stuff)
-
-
-
-
-
-
File: <ipython-input-4-dae73707787c>
-
Function: do_other_stuff at line 4
-
-
-
==============================================================
-
4 def do_other_stuff(numbers):
-
5 1 9 9.0 100.0 s = sum(numbers)
-
-
-
File: <ipython-input-4-dae73707787c>
-
Function: do_stuff at line 7
-
-
-
==============================================================
-
-
8 1 12 12.0 1.7 do_other_stuff(numbers)
-
9 1 208 208.0 30.0 l = [numbers[i]/43 for i in range(len(numbers))]
-
10 1 474 474.0 68.3 m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
结束
阅读(925) | 评论(0) | 转发(0) |