Python memory_profiler 不一致的绘图

标签 python memory plot profiler memory-profiling

我最近开始使用 here 中的 python 内存分析器。作为测试运行,我尝试分析 here 中的玩具代码。按照其中的说明进行操作。我对我看到的输出有一些天真的问题。

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 然后绘制命令行选项的输出: With function profiling

删除@profile行后,我再次运行分析器并获得以下结果: Without function profiling

除了函数的括号之外,我期望得到几乎相同的图(因为代码很简单),但我看到了一些显着的差异,例如图的结束时间、括号内的变化等。

有人可以阐明这些差异吗?

编辑: 对于小间隔,函数分析图如下所示: small intervals

最佳答案

您看到的差异可能是由于 @profile 存储的信息计入程序使用的总内存中。存储这些信息也会产生轻微的开销,因此运行时间也不同。

此外,由于 Python 管理内存的方式不同,您可能会在不同的运行中得到略有不同的图。

关于Python memory_profiler 不一致的绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31732187/

相关文章:

Android RAM 过度使用

python - pandas 0.21.0 时间戳与 matplotlib 的兼容性问题

python - Hindmarsh-Rose 模型的相空间轨迹

python - xlsxwriter 未将格式应用于数据框的标题行 - Python Pandas

python - Scrapy从第一个元素和帖子标题收集数据

Python:替换 DataFrame 中的 "{}"

python - 将枚举类型作为构造函数参数传递

c++ - 之后填充 std::unique_ptr

c++ - 段错误错误 C++

Python:在 Matplotlib 中绘制均匀分布的球体