这里,就生成了与上面一致的信息
>>> p.print_callers() Ordered by: cumulative time Function was called by... ncalls tottime cumtime {built-in method builtins.exec} <- code.py:1(<module>) <- 1 0.060 30.461 {built-in method builtins.exec} code.py:35(calc_pure_python) <- 1 1.509 30.400 code.py:1(<module>) code.py:13(measure_time) <- 1 0.000 28.516 code.py:35(calc_pure_python) code.py:22(calculate_z_serial_purepython) <- 1 20.032 28.515 code.py:13(measure_time) {built-in method builtins.abs} <- 34219980 8.483 8.483 code.py:22(calculate_z_serial_purepython) {method 'append' of 'list' objects} <- 2002000 0.360 0.360 code.py:35(calc_pure_python) {built-in method builtins.sum} <- 1 0.012 0.012 code.py:35(calc_pure_python) {built-in method builtins.print} <- 1 0.000 0.000 code.py:13(measure_time) 3 0.003 0.003 code.py:35(calc_pure_python) code.py:12(timefn) <- 1 0.000 0.000 code.py:1(<module>) C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:44(update_wrapper) <- 1 0.000 0.000 code.py:12(timefn) {built-in method time.time} <- 2 0.000 0.000 code.py:13(measure_time) 2 0.000 0.000 code.py:35(calc_pure_python) <frozen importlib._bootstrap>:989(_handle_fromlist) <- 1 0.000 0.000 code.py:1(<module>) {built-in method builtins.getattr} <- 7 0.000 0.000 C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:44(update_wrapper) {built-in method builtins.hasattr} <- 1 0.000 0.000 <frozen importlib._bootstrap>:989(_handle_fromlist) {built-in method builtins.len} <- 2 0.000 0.000 code.py:22(calculate_z_serial_purepython) 2 0.000 0.000 code.py:35(calc_pure_python) C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:74(wraps) <- 1 0.000 0.000 code.py:12(timefn) {method 'update' of 'dict' objects} <- 1 0.000 0.000 C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:44(update_wrapper) {built-in method builtins.setattr} <- 5 0.000 0.000 C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:44(update_wrapper) {method 'disable' of '_lsprof.Profiler' objects} <- <pstats.Stats object at 0x000002AA0A6A8908>这里,我们可以看到,在每一行最后会有调用这部分的父函数名称,这样我们就可以定位到对某一操作最费时的那个函数。
我们还可以显示那个函数调用了其它函数
>>> p.print_callees() Ordered by: cumulative time Function called... ncalls tottime cumtime {built-in method builtins.exec} -> 1 0.060 30.461 code.py:1(<module>) code.py:1(<module>) -> 1 0.000 0.000 <frozen importlib._bootstrap>:989(_handle_fromlist) 1 0.000 0.000 code.py:12(timefn) 1 1.509 30.400 code.py:35(calc_pure_python) code.py:35(calc_pure_python) -> 1 0.000 28.516 code.py:13(measure_time) 2 0.000 0.000 {built-in method builtins.len} 3 0.003 0.003 {built-in method builtins.print} 1 0.012 0.012 {built-in method builtins.sum} 2 0.000 0.000 {built-in method time.time} 2002000 0.360 0.360 {method 'append' of 'list' objects} code.py:13(measure_time) -> 1 20.032 28.515 code.py:22(calculate_z_serial_purepython) 1 0.000 0.000 {built-in method builtins.print} 2 0.000 0.000 {built-in method time.time} code.py:22(calculate_z_serial_purepython) -> 34219980 8.483 8.483 {built-in method builtins.abs} 2 0.000 0.000 {built-in method builtins.len} {built-in method builtins.abs} -> {method 'append' of 'list' objects} -> {built-in method builtins.sum} -> {built-in method builtins.print} -> code.py:12(timefn) -> 1 0.000 0.000 C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:44(update_wrapper) 1 0.000 0.000 C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:74(wraps) C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:44(update_wrapper) -> 7 0.000 0.000 {built-in method builtins.getattr} 5 0.000 0.000 {built-in method builtins.setattr} 1 0.000 0.000 {method 'update' of 'dict' objects} {built-in method time.time} -> <frozen importlib._bootstrap>:989(_handle_fromlist) -> 1 0.000 0.000 {built-in method builtins.hasattr} {built-in method builtins.getattr} -> {built-in method builtins.hasattr} -> {built-in method builtins.len} -> C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:74(wraps) -> {method 'update' of 'dict' objects} -> {built-in method builtins.setattr} -> {method 'disable' of '_lsprof.Profiler' objects} -> <pstats.Stats object at 0x000002AA0A6A8908> line_profiler 逐行分析前面我们通过 cProfile 来对代码进行了整体的分析,当我们确定了耗时多的函数后,想对该函数进行进一步分析时,就可以使用 line_profiler 了。
先安装
pip install line_profiler 或 conda install line_profiler在需要测试的函数前面加上修饰器 @profile,然后命令函输入
kernprof -l -v code.py