【问题标题】:python cprofile shows lot of information. Can it be limited to only my codepython cprofile 显示了很多信息。可以仅限于我的代码吗
【发布时间】:2021-09-21 07:40:30
【问题描述】:

cProfile 在输出中显示了许多内置函数调用。我们可以将输出限制为我编写的代码吗?因此,在下面的示例中,我是否只能看到来自testrun 的行或来自位于同一脚本中的testrun() 调用的函数。或者可能将记录的呼叫级别限制为 2 或 3?

pr = cProfile.Profile()
pr.enable()
testrun()
pr.disable()
pr.print_stats(sort='time')

【问题讨论】:

    标签: python cprofile


    【解决方案1】:

    您可以过滤输出,如this question所示。

    因此,例如,您可以按模块名称、c.f.、print_stats() 进行过滤:

    pr.sort_stats("time").print_stats("dir_or_module_name")  # replace dir_or_module_name
    

    请确保之前调用strip_dirs(),因为这可能会删除您的目录/模块名称。


    编辑:您甚至可以按多个名称进行过滤,因为print_stats() 应该接受正则表达式:

    pr.print_stats("dir1|dir2|module1")
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2013-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-22
      相关资源
      最近更新 更多