写一手漂亮的代码,走向极致的编程 一、代码运行时间分析 (2)

输出,WSL 的 time 命令里面没有 --verbose 这个参数,只能到服务器里面试了,突然觉得我的笔记本跑的好慢。。。

Length of x: 1000 Total elements: 1000000 @timefn:calculate_z_serial_purepython took 7.899603605270386 seconds calculate_z_serial_purepython took 7.899857997894287 seconds Command being timed: "python code.py" User time (seconds): 8.33 System time (seconds): 0.08 Percent of CPU this job got: 98% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:08.54 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 98996 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 0 Minor (reclaiming a frame) page faults: 25474 Voluntary context switches: 0 Involuntary context switches: 2534 Swaps: 0 File system inputs: 0 File system outputs: 0 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0

这里面需要关心的参数是 Major (requiring I/O) page faults ,表示操作系统是否由于 RAM 中的数据不存在而需要从磁盘上读取页面。

cProfile 模块

cProfile 模块是标准库内建三个的分析工具之一,另外两个是 hotshot 和 profile。

python -m cProfile -s cumulative code.py

-s cumulative 表示对每个函数累计花费的时间进行排序

输出

36222017 function calls in 30.381 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 30.381 30.381 {built-in method builtins.exec} 1 0.064 0.064 30.381 30.381 code.py:1(<module>) 1 1.365 1.365 30.317 30.317 code.py:35(calc_pure_python) 1 0.000 0.000 28.599 28.599 code.py:13(measure_time) 1 19.942 19.942 28.598 28.598 code.py:22(calculate_z_serial_purepython) 34219980 8.655 0.000 8.655 0.000 {built-in method builtins.abs} 2002000 0.339 0.000 0.339 0.000 {method 'append' of 'list' objects} 1 0.012 0.012 0.012 0.012 {built-in method builtins.sum} 4 0.003 0.001 0.003 0.001 {built-in method builtins.print} 1 0.000 0.000 0.000 0.000 code.py:12(timefn) 1 0.000 0.000 0.000 0.000 functools.py:44(update_wrapper) 4 0.000 0.000 0.000 0.000 {built-in method time.time} 1 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:989(_handle_fromlist) 4 0.000 0.000 0.000 0.000 {built-in method builtins.len} 7 0.000 0.000 0.000 0.000 {built-in method builtins.getattr} 1 0.000 0.000 0.000 0.000 {built-in method builtins.hasattr} 5 0.000 0.000 0.000 0.000 {built-in method builtins.setattr} 1 0.000 0.000 0.000 0.000 functools.py:74(wraps) 1 0.000 0.000 0.000 0.000 {method 'update' of 'dict' objects} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}

可以看到,在代码的入口处总共花费了 30.381 秒,ncalls 为 1,表示只执行了 1 次,然后 calculate_z_serial_purepython 花费了 28.598 秒,可以推断出调用该函数使用了近 2 秒。另外可以看到,abs 函数被调用了 34219980 次。对列表项的 append 操作进行了 2002000 次(1000 * 1000 * 2 +1000 * 2 )。

接下来,我们进行更深入的分析。

python -m cProfile -o profile.stats code.py

先生成一个统计文件,然后在 python 中进行分析

>>> import pstats >>> p = pstats.Stats("profile.stats") >>> p.sort_stats("cumulative") <pstats.Stats object at 0x000002AA0A6A8908> >>> p.print_stats() Sat Apr 25 16:38:07 2020 profile.stats 36222017 function calls in 30.461 seconds Ordered by: cumulative time ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 30.461 30.461 {built-in method builtins.exec} 1 0.060 0.060 30.461 30.461 code.py:1(<module>) 1 1.509 1.509 30.400 30.400 code.py:35(calc_pure_python) 1 0.000 0.000 28.516 28.516 code.py:13(measure_time) 1 20.032 20.032 28.515 28.515 code.py:22(calculate_z_serial_purepython) 34219980 8.483 0.000 8.483 0.000 {built-in method builtins.abs} 2002000 0.360 0.000 0.360 0.000 {method 'append' of 'list' objects} 1 0.012 0.012 0.012 0.012 {built-in method builtins.sum} 4 0.004 0.001 0.004 0.001 {built-in method builtins.print} 1 0.000 0.000 0.000 0.000 code.py:12(timefn) 1 0.000 0.000 0.000 0.000 C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:44(update_wrapper) 4 0.000 0.000 0.000 0.000 {built-in method time.time} 1 0.000 0.000 0.000 0.000 <frozen importlib._bootstrap>:989(_handle_fromlist) 7 0.000 0.000 0.000 0.000 {built-in method builtins.getattr} 1 0.000 0.000 0.000 0.000 {built-in method builtins.hasattr} 4 0.000 0.000 0.000 0.000 {built-in method builtins.len} 1 0.000 0.000 0.000 0.000 C:\Users\ITryagain\AppData\Local\conda\conda\envs\tensorflow-gpu\lib\functools.py:74(wraps) 1 0.000 0.000 0.000 0.000 {method 'update' of 'dict' objects} 5 0.000 0.000 0.000 0.000 {built-in method builtins.setattr} 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects} <pstats.Stats object at 0x000002AA0A6A8908>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwyzxj.html