Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 内存分析器:如何绘制每个函数的内存使用情况_Python_Memory Profiling - Fatal编程技术网

Python 内存分析器:如何绘制每个函数的内存使用情况

Python 内存分析器:如何绘制每个函数的内存使用情况,python,memory-profiling,Python,Memory Profiling,版本:0.50.0 在公园里。这里有一个链接,告诉我如何绘制每个函数的内存使用图。但是当我试图运行本文中的代码时 test1.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

版本:0.50.0

在公园里。这里有一个链接,告诉我如何绘制每个函数的内存使用图。但是当我试图运行本文中的代码时

test1.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
我得到这个错误:

回溯(最近一次呼叫最后一次):

文件“test.py”,第3行,在 @侧面图 NameError:未定义名称“配置文件”

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

警告

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


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

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

谢谢。它是有用的。我还在github上提交了一个bug。我将分享你的答案,也许可以帮助他们解决问题。如果有人带着同样的问题来这里尝试运行pytest,它会起作用;不要使用
python3
而是使用
pytest
mprof运行--python-pytest路径到测试文件
这是v0.50中的一个错误,应该在当前的v0.51中修复