【问题标题】:How to omit methods in cProfile如何省略 cProfile 中的方法
【发布时间】:2021-10-12 17:27:55
【问题描述】:

当我分析大量使用 all/any 内置函数的代码时,我发现分析工具(如 gprof2dot)生成的调用图可能更难以解释,因为来自不同调用者的许多边导致 all/任何节点,以及许多引出的边。我正在寻找一种从调用图中基本上省略所有/任何节点的方法,这样在这个非常简化的示例中,两个代码路径不会收敛然后分裂。

import cProfile
import random
import time

def bad_true1(*args, **kwargs):
    time.sleep(random.random() / 100)
    return True

def bad_true2(*args, **kwargs):
    time.sleep(random.random() / 100)
    return True

def code_path_one():
    nums = [random.random() for _ in range(100)]
    return all(bad_true1(x) for x in nums)


def code_path_two():
    nums = [random.random() for _ in range(100)]
    return all(bad_true2(x) for x in nums)


def do_the_thing():
    code_path_one()
    code_path_two()


def main():
    profile = OmittingProfile()
    profile.enable()
    do_the_thing()
    profile.disable()
    profile.dump_stats("foo.prof")


if "__main__" == __name__:
    main()

【问题讨论】:

    标签: python profiling cprofile


    【解决方案1】:

    我不认为 cProfile 提供了一种在收集时过滤掉函数的方法。您可能可以在获得统计信息后手动过滤掉函数,但这比您想做的要多。

    另外,根据我的经验,只要有很多嵌套调用,cProfile 只会有助于找到“最耗时的函数”,仅此而已,没有额外的上下文信息,因为 cProfile 只记录其父级函数,而不是整个调用堆栈。对于复杂的程序,这可能不是很有帮助。

    基于上述原因,我建议您尝试其他一些分析工具。例如,viztracer。 VizTracer 绘制出您的整个程序执行情况,以便您了解程序中发生的情况。而且它恰好有过滤掉内置函数的能力。

    pip install viztracer
    # --ignore_c_function is the optional filter
    viztracer --ignore_c_function your_program.py
    vizviewer result.json
    

    当然,还有其他提供火焰图的统计分析器,它包含的信息较少,但也引入了较少的开销,如 pyspy、scalene。

    cProfile 是用于一些简单分析的好工具,但它绝对不是市场上最好的工具。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-19
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      相关资源
      最近更新 更多