【问题标题】:How to make cProfile print only significant functions?如何使 cProfile 仅打印重要功能?
【发布时间】:2017-11-30 02:05:55
【问题描述】:

我从我的 cProfile 输出中获得了大约 300 个条目,并且每次使用它时都必须向上滚动很长时间。

有没有办法让 cProfile 只打印前 10 行之类的东西?

【问题讨论】:

  • @sam-pyt 你能详细说明一下吗?
  • 重点是显示程序的所有不同部分,执行需要多长时间以及执行频率。
  • @sam-pyt 但我不关心几乎没有执行的功能。我正在整理我的结果,因此只有最重要的对我来说很重要。
  • @snakecharmerb 我按时间排序。

标签: python performance cprofile


【解决方案1】:

您可以使用 print_stats(4) 按“累积”排序并显示前 N 行。如果不对某些内容进行排序,则可能无法使用前 N 行。

来自Python profiler official docs

import pstats
p = pstats.Stats('restats')
p.sort_stats('cumulative').print_stats(10)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多