Python性能分析与NumPy入门指南
1. IPython性能分析工具
1.1 %prun与%lprun
在进行算法性能优化时,我们可以借助IPython的性能分析工具。%prun可以进行函数级别的性能分析,若想获取逐行的性能报告,可使用%lprun。不过,%lprun并非Python或IPython内置功能,需要先安装 line_profiler 包:
$ pip install line_profiler
安装完成后,在IPython中加载该扩展:
%load_ext line_profiler
使用%lprun对 sum_of_lists 函数进行逐行性能分析:
%lprun -f sum_of_lists sum_of_lists(5000)
分析结果示例如下:
Timer unit: 1e-06 s
Total time: 0.009382 s
File: <ipython-input-19-fa2be176cc3e>
Function: sum_of_lists at line 1
Line # Hits Time Per Hit % Tim
超级会员免费看
订阅专栏 解锁全文
2964

被折叠的 条评论
为什么被折叠?



