基准测试与剖析
研究高性能之前要先找到计算运行时间的方法.
1、linux的time 命令
time python test.py
real 0m1.051s : 从头到尾运行进程实际花费的时间
user 0m1.022s :在计算期间,所有CPU话费的时间
sys 0m0.028s : 在执行与系统相关的任务(如内存分配)期间,所有CPU花费的总时间。
time 为Linux的命令,所以这种办法可以与语言无关。
2、python 的 timeit 模块
为了测量python脚本的执行时间,另一种方法是使用 timeit 模块
python -m timeit -s 'from simul import benchmark' 'benchmark()'
3、CProfile 剖析器
python -m cProfile simul.py
4、line_profiler 剖析
5、使用memory_profiler剖析内存使用情况
通过性能分析之后,找到运行慢的代码,然后优化,对比优化前后的运行时间,看到实际的优化效果。