安装
首先安装memory_profiler和psutil
pip install memory_profiler
pip install psutil
- 在需要分析的函数前面添加装饰器@profile
@profile()
def call():
a = list()
for i in range(10000 * 100):
a .append(i)
运行call后的输出:
Line # Mem usage Increment Line Contents
================================================
283 40.1 MiB 40.1 MiB @profile
284 def call():
285 40.1 MiB 0.0 MiB a = list()
286 44.1 MiB 0.0 MiB for i in range(10000 * 10):
287 44.1 MiB 0.3 MiB a .append(i)