函数性能统计

函数性能统计

https://superfastpython.com/benchmark-python-code/#Benchmark_with_cProfile

第五章 详细阅读,能够列出每个函数的时间,以及函数中调用的函数的性能

 

profile 模块使用参考

Python性能分析工具Profile - -零 - 博客园 (cnblogs.com)

函数支持保存日志参数

cProfile模块 使用方法

1)支持某一处函数统计

Python 程序测试 profile and Cprofile-CSDN博客

 

一 性能(时间)分析

  参考文档:【调优工具】python性能分析工具cProfiler_python cprofiler 用法-CSDN博客,主要使用cprofiler例子

 

二更多性能分析工具

    line_profiler

      保存性能报告 # lpf.dump_stats('save_name.lprof')

      读取报告        # python -m line_profiler save_name.lprof

  如何对line_profiler排序? 由官网学习:line-profiler · PyPI

  

  版本1-基于line_profiler 分析每行代码时间-排序

   排序代码

def get_sorted_lpf(obj, k_tuple, topk):
    """
    obj 为保存的标准打印 字典对象,里面保存了hits times line
    k_tuple,obj字典用元组作为键
    topk,排序后选出topk
    """
    col = []
    tab = obj.__dict__['timings'][k_tuple]
    # [(14, 1, 1788), (15, 1, 60235833), (16, 1, 2224), (17, 1, 40), (18, 1, 2537)]
    for i in tab:
        td = {}
        td["line"] = i[0]
        td["hits"] = i[1]
        td["times"] = i[2]/10000000
        col.append(td)
    print(col)
    from operator import itemgetter
    # 按照键对字典进行排序
    sorted_data = sorted(col, key=itemgetter('times'),reverse=True)

    # 输出排序后的结果
    for item in sorted_data[:topk]:
        print(item)
    return sorted_data

  完整实例代码

import re
import time
from line_profiler import LineProfiler
def _swait():
    time.sleep(2)
    print("---swait---")

def get_str(obj, n):
    for i in range(n):
        _swait()
        re.search("-j-", obj)

def main(n):
    print("1111111111111")
    get_str("out-j0-j-a", n)
    print("2222222222222")
    time.time()
    re.compile("out-j0-j-a")

def get_sorted_lpf(obj, k_tuple, topk):
    """
    obj 为保存的标准打印 字典对象,里面保存了hits times line
    k_tuple,obj字典用元组作为键
    topk,排序后选出topk
    """
    col = []
    tab = obj.__dict__['timings'][k_tuple]
    # [(14, 1, 1788), (15, 1, 60235833), (16, 1, 2224), (17, 1, 40), (18, 1, 2537)]
    for i in tab:
        td = {}
        td["line"] = i[0]
        td["hits"] = i[1]
        td["times"] = i[2]/10000000
        col.append(td)
    print(col)
    from operator import itemgetter
    # 按照键对字典进行排序
    sorted_data = sorted(col, key=itemgetter('times'),reverse=True)

    # 输出排序后的结果
    for item in sorted_data[:topk]:
        print(item)
    return sorted_data


if __name__ == "__main__":
    lpf = LineProfiler()
    lpf.add_function(get_str)
    tfunc = lpf(main)
    tfunc(3)
    lpf.print_stats(sort=True)
    # lpf.print_stats(sort_by_time=True)
    # lpf.print_stats(sortby='cumulative')
    lpf.dump_stats('save_name.lprof')
    # lpf.dump_stats('save_name.prof')

    obj = lpf.get_stats()
    k_tup = ('C:\\Users\\xialiu05\\Documents\\公司任务\\ecosys\\GPT2-large\\chat\\timeparsetool\\lineparsetool.py', 13, 'main')
    get_sorted_lpf(obj, k_tup, 10)
    # import pstats
    # p = pstats.Stats('./save_name.prof')
    # p.strip_dirs().sort_stats('Time').print_stats(10)

  

对于实例化的实例,调用了某个函数,在line_profiler中可能被统计两遍。

类似代码

     lf = ListenFile(waitnoneepoch=2000,
                     waitnonetime=0.002,
                     targetfilesize=1331700)
     logits = lf.listen_targetdir_withcpp(abs_out_dir,
                                                 "output.bin",
                                                 # epch=10,
                                                 # querytime=1,
                                                 epch=2000,
                                                 querytime=0.002,
                                                 # querytime=0.001,
                                                 readfunc_pad=args.padding,
                                                 readfunc_vocleng=13317,
                                                 readfunc_astypes=np.int16,
                                                 readfunc_backquant=True,
                                                 readfunc_scale=1381.2528076171875,
                                                 readfunc=read_output)    

  

 这478行即被统计双倍。

times 总的耗时是正确的。

所以求pertime 需要hits/2=实际的hits

 

 

      

 

posted on 2023-10-19 10:35  lexn  阅读(8)  评论(0编辑  收藏  举报

导航