【问题标题】:memory_profiler display only function level resultsmemory_profiler 仅显示函数级结果
【发布时间】:2016-12-15 04:20:27
【问题描述】:

我使用memory_profiler for python如下:

from memory_profiler import profile

@profile
def my_func():
   ...

并正常运行。

输出如下所示(取自文档):

Line #    Mem usage  Increment   Line Contents
==============================================
     3                           @profile
     4      5.97 MB    0.00 MB   def my_func():
     5     13.61 MB    7.64 MB       a = [1] * (10 ** 6)
     6    166.20 MB  152.59 MB       b = [2] * (2 * 10 ** 7)
     7     13.61 MB -152.59 MB       del b
     8     13.61 MB    0.00 MB       return a

虽然我希望将其缩短为功能级别并类似于以下内容:

Line #    Mem usage  Increment    Max         Line Contents
==============================================================
     4      13.61 MB    7.64 MB   166.20 MB     my_func()
     20     23.26 MB    9.65 MB   56.20 MB     my_second_func()

这可能吗?

【问题讨论】:

标签: python-2.7 memory-profiling


【解决方案1】:

你总是可以定义第三个函数

@profile
def my_third_func():
    my_func()
    my_second_func()

并分析此功能。这应该会产生您正在寻找的输出。

否则,如果您只对不同函数的最大内存消耗感兴趣,mprof 提供了一个方便的可视化图表。

【讨论】:

    猜你喜欢
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多