python时间监测工具line_profiler
时间监测工具line_profiler
ine_profiler是Python的一个第三方库,其功能时基于函数的逐行代码分析工具。通过该库,可以对目标函数允许分析多个函数)进行时间消耗分析,便于代码调优。
安装
pip install line_profiler
部分注释
Timer unit: 1e-07 s
Total time: 7.13e-05 s
File: d:\Note\lcodeNoteCards\testcode\python\testpy.py
Function: do_stuff at line 4
Line # Hits Time Per Hit % Time Line Contents
==============================================================
4 def do_stuff(numbers):
5 1 24.0 24.0 3.4 s = sum(numbers)
6 1 249.0 249.0 34.9 l = [numbers[i]/43 for i in range(len(numbers))]
7 1 437.0 437.0 61.3 m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
8 1 3.0 3.0 0.4 return s, l, m
Timer unit:分析表中,Time和Per Hit的数值单位,时间单位固定为秒(s),默认数值单位是1e-6,合在一起便是1e-6s,即微秒。实际某一行的运行时间是Time * Timer unit的值,觉得不好理解的话,下方介绍kernprof命令时有一个示例供各位同学理解,这里只是简单提一下。
Total time:当前函数的时间消耗,单位是秒。
File:当前函数所在文件名。
Function:当前函数的函数名以及在文件中的位置。
Line #:代码所在行号。
Hits:在执行过程中,该行代码执行次数,即命中数。
Time:在执行过程中,该行代码执行的总时间,默认单位是微秒。
Per Hit:在执行过程中,平均每次执行该行代码所耗时间,默认单位是微秒。
% Time:执行该行代码所耗总时间占执行当前函数所耗总时间的百分比。
Line Contents:该行代码的内容。
使用方法
# line_profiler_test.py
from line_profiler import LineProfiler
import numpy as np
@profile
def test_profiler():
for i in range(100):
a = np.random.randn(100)
b = np.random.randn(1000)
c = np.random.randn(10000)
return None
if __name__ == '__main__':
test_profiler()
kernprof -v -l "d:\Note\lcodeNoteCards\testcode\python\testpy.py"
Wrote profile results to testpy.py.lprof
Timer unit: 1e-06 s
Total time: 0.0209844 s
File: d:\Note\lcodeNoteCards\testcode\python\testpy.py
Function: test_profiler at line 5
Line # Hits Time Per Hit % Time Line Contents
==============================================================
5 @profile
6 def test_profiler():
7 101 25.8 0.3 0.1 for i in range(100):
8 100 362.6 3.6 1.7 a = np.random.randn(100)
9 100 1998.9 20.0 9.5 b = np.random.randn(1000)
10 100 18596.8 186.0 88.6 c = np.random.randn(10000)
11 1 0.3 0.3 0.0 return None
同时显示内部函数
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-07 s
Total time: 7e-06 s
File: d:\Note\lcodeNoteCards\testcode\python\testpy.py
Function: do_other_stuff at line 4
Line # Hits Time Per Hit % Time Line Contents
==============================================================
4 def do_other_stuff(numbers):
5 1 70.0 70.0 100.0 s = sum(numbers)
Total time: 0.0006311 s
File: d:\Note\lcodeNoteCards\testcode\python\testpy.py
Function: do_stuff at line 7
Line # Hits Time Per Hit % Time Line Contents
==============================================================
7 def do_stuff(numbers):
8 1 104.0 104.0 1.6 do_other_stuff(numbers)
9 1 2064.0 2064.0 32.7 l = [numbers[i]/43 for i in range(len(numbers))]
10 1 4143.0 4143.0 65.6 m = ['hello'+str(numbers[i]) for i in range(len(numbers))]
参考资料
https://blog.csdn.net/weixin_42245157/article/details/125415104