版本:0.50.0
在official documentation .有指向此 blog 的链接这给了我如何制作每个函数内存使用图的说明。但是当我尝试运行这篇文章中的代码时。
测试1.py
import time
@profile
def test1():
n = 10000
a = [1] * n
time.sleep(1)
return a
@profile
def test2():
n = 100000
b = [1] * n
time.sleep(1)
return b
if __name__ == "__main__":
test1()
test2()
命令是:
mprof run test1.py
我收到这个错误:
Traceback (most recent call last):
File "test.py", line 3, in @profile NameError: name 'profile' is not defined
这很奇怪,因为有官方的引述:
Warning
If your Python file imports the memory profiler from memory_profiler import profile these timestamps will not be recorded. Comment out the import, leave your functions decorated, and re-run.
因此,如果我想要每个函数的内存使用情况图,我需要注释掉 from memory_profiler import profile
,但是当我注释掉它时,出现错误。
最佳答案
文档不是最新的,尝试使用 mprof run --python python3 test1.py
(注释掉导入),它似乎对我有用,但会生成输出到文件仅且根本不写入标准输出。
关于python - 内存分析器 : how to plot per-function memory usage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48061659/