python 代码逐行耗时分析


@profile
def test():
    print(1+1)

if __name__ == '__main__':
    test()
    
    
'''

pip install line_profiler

kernprof -l -v test.py 

-l 逐行分析
-v 输出

Wrote profile results to test.py.lprof
Timer unit: 1e-06 s

Total time: 0.0002118 s
File: test.py
Function: test at line 2

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
     2                                           @profile
     3                                           def test():
     4         1        211.8    211.8    100.0      print(1+1)
'''