【问题标题】:Using Yappi for profiling使用 Yappi 进行分析
【发布时间】:2017-07-02 20:43:53
【问题描述】:

当我使用 cProfiler 时,我得到以下行:

 ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     39   12.486    0.320   12.486    0.320 {method 'acquire' of 'thread.lock' objects}

understood 认为 yappi 是要走的路。

所以我在写:

yappi.get_func_stats().print_all()

而且我的行数太多了。

如何只检索最耗时的 10 个?

相当于:

p.sort_stats('time').print_stats(10)

我基本上想知道什么最消耗时间。

我确实使用ThreadPoolExecutor 在我的代码中运行线程

【问题讨论】:

  • 检查this,或者你也可以使用GDB。
  • 嗨,迈克,你能解释一下吗?我不确定它如何帮助我解决问题。这就像成千上万条线路可供探索。我非常了解代码
  • 可能有数十亿行。它越大,狩猎越好。很高兴您对它非常了解,因为您会看到它在两个或更多样本上做一些可以做得更好的事情。那是您的(第一次)加速。修复它并获得加速,您事先不知道其大小,但遵循inverse beta distribution。找到它所需的样本越少,加速就越大。冲洗,重复。它找到了加速分析器找到的超集。

标签: python profiler


【解决方案1】:

如果你想限制结果,你只能修改排序,你必须修改print_all方法

用于排序统计数据

import sys
from yappi import get_func_stats, COLUMNS_FUNCSTATS, COLUMNS_THREADSTATS
# Stats sorted by total time
stats = get_func_stats.sort(
        sort_type='totaltime', sort_order='desc') 
# returns all stats with sorting applied 
print_all(stats, sys.stdout, limit=10)

修改后的打印

import os
def print_all(stats, out, limit=None):
    if stats.empty():
        return
    sizes = [36, 5, 8, 8, 8]
    columns = dict(zip(range(len(COLUMNS_FUNCSTATS)), zip(COLUMNS_FUNCSTATS, sizes)))
    show_stats = stats
    if limit:
        show_stats = stats[:limit]
    out.write(os.linesep)
    # write out the headers for the func_stats
    # write out stats with exclusions applied.
    # for stat in show_stats:
    #    stat._print(out, columns)  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2022-01-24
    • 2017-08-16
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多