python - 使用 Yappi 进行分析

标签 python profiler

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

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

understoodyappi是要走的路。

所以我在写:

yappi.get_func_stats().print_all()

而且我有太多行要阅读。

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

相当于:

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

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

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

最佳答案

如果你想限制你必须修改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)  

关于python - 使用 Yappi 进行分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42228201/

相关文章:

python - Django:如何根据其子项的子项数量来订购一个集合?

android - Profiler 中的内存使用量比实际设备少得多

java - 从JTextArea中删除Text后,堆不为空

Java 分析 - 检测导致峰值的原因

python - 插入/并将 yymmdd 反转为 ddmmyy

python - 使用命名空间实现 Multi-Tenancy (Python 3+)

Python:使用超过十亿行的输入加速脚本

python - 将文件从未知文件夹移动到其他文件夹

java - VisualVM 探查器错误 : "Class JavaLaunchHelper is implemented in both ..bin/java and ..jre/lib"

Nhibernate 探查器显示警告 "Use explicit Save or Update in session"