memory_profiler:如何绘制每个函数的内存使用情况

sco*_*ang 3 python memory-profiling

版本:0.50.0

官方文档中。该博客有一个链接,它指导我如何制作每个函数的内存使用情况图。但是当我尝试运行本文中的确切代码时。

测试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()
Run Code Online (Sandbox Code Playgroud)

命令是:

mprof run test1.py
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

回溯(最近一次调用最后一次):

文件“test.py”,第 3 行,@profile NameError:名称“profile”未定义

这很奇怪,因为有官方的引用:

警告

如果您的 Python 文件从 memory_profiler import profile 导入内存分析器,则不会记录这些时间戳。注释掉导入,保留您的函数,然后重新运行。

因此,如果我想要每个函数的内存使用情况图,我需要注释掉 from memory_profiler import profile,但是当我注释掉它时,出现错误。

erh*_*sto 5

文档并不是真正最新的,尝试使用mprof run --python python3 test1.py(已注释掉导入),它似乎对我有用,但仅生成输出到文件,根本不写入标准输出。