python - 内存分析器 : how to plot per-function memory usage

标签 python memory-profiling

版本: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/

相关文章:

java getBytes() 在 python 中等效

Python Docx 回车

c# - .Net 内存分析器和 <GCHandle> 固定

profiling - 如何在 Swift 中检测和调试强引用循环?

python - 如何在 Python 中对单元测试进行时间分析?

python - 让 TensorFlow 使用由自定义 CUDA 例程即时生成的训练数据

python - 找到使 pandas DataFrame 中相关性更差的最差元素

c# - VS2013 : Memory profiler doesn't show anything on a specific project

python import 执行本地文件