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

时间:2022-10-31 10:56:39

python性能调试过程中最突出的问题就是耗时,性能测试工具有很多,像profiler,cprofiler等等,都是只能返回函数整体的耗时,而line_profiler就能够很好解决这个问题(大家可以试试就知道了)。

怎么使用这个工具呢?网上大部分都是说在所需要测的函数前面加一个@profile,如文档所说。但是加了@profile后函数无法直接运行,只能优化的时候加上,调试的时候又得去掉。文章中提到了这个问题的解决办法,个人觉得还是有点麻烦,不太能理解这是为什么。我在*上看到了另一种关于line_profile的使用方法,简单而且实用。

这是使用案例:

from line_profiler import LineProfiler import random 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))] numbers = [random.randint(1,100) for i in range(1000)] lp = LineProfiler() lp_wrapper = lp(do_stuff) lp_wrapper(numbers) lp.print_stats()
输出结果:

Timer unit: 1e-06 s Total time: 0.000649 s File: <ipython-input-2-2e060b054fea> Function: do_stuff at line 4 Line # Hits Time Per Hit % Time Line Contents ============================================================== 4 def do_stuff(numbers): 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 import random def do_other_stuff(numbers): s = sum(numbers) def do_stuff(numbers): 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 = LineProfiler() lp_wrapper = lp(do_stuff) lp_wrapper(numbers) lp.print_stats()
这样做的话,只能显示子函数的总时间。输出如下:

Timer unit: 1e-06 s Total time: 0.000773 s File: <ipython-input-3-ec0394d0a501> Function: do_stuff at line 7 Line # Hits Time Per Hit % Time Line Contents ============================================================== 7 def do_stuff(numbers): 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 import random def do_other_stuff(numbers): s = sum(numbers) def do_stuff(numbers): 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 = LineProfiler() lp.add_function(do_other_stuff) # add additional function to profile lp_wrapper = lp(do_stuff) lp_wrapper(numbers) lp.print_stats()
Timer unit: 1e-06 s Total time: 9e-06 s File: <ipython-input-4-dae73707787c> Function: do_other_stuff at line 4 Line # Hits Time Per Hit % Time Line Contents ============================================================== 4 def do_other_stuff(numbers): 5 1 9 9.0 100.0 s = sum(numbers) Total time: 0.000694 s File: <ipython-input-4-dae73707787c> Function: do_stuff at line 7 Line # Hits Time Per Hit % Time Line Contents ============================================================== 7 def do_stuff(numbers): 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))]
结束