c+=1 10 21.492 MiB 0.000 MiB print c Mem usage: 内存占用情况 Increment: 执行该行代码后新增的内存 运行时间 line-profiler [2] 安装 pip install line-profiler 使用 在需要分析的函数上,添加装饰器@profile @profile def slow_function(a, b, c): function for display. ---- 参考资料 [1] memory-profiler: https://pypi.org/project/memory-profiler/ [2] line-profiler
性能分析工具不够完善,基于cprofile开发的,缺少line-profile更细致的性能分析,如果能结合cprofile的功能定位出大致慢的函数,然后可以方便的用line-profiler定位具体慢的行
下面这个例子展示了memory-profiler是如何工作的(注意装饰器和line-profiler类似) @profile def my_func(): a = [1] * (10 ** 6)
/site-packages (from traitlets>=4.2->IPython->line_profiler) (0.2.0) Installing collected packages: line-profiler