函数性能统计
函数性能统计
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例子
二更多性能分析工具
-
cProfiler
统计在整个代码执行过程中,每个函数调用的次数和消耗的时间 -
line_profiler
分析每一行代码的运行时间,方便定位程序运行效率瓶颈 -
memory_profiler
分析每行代码的内存使用情况,以进行内存消耗分析
line_profiler
保存性能报告 # lpf.dump_stats('save_name.lprof')
读取报告 # python -m line_profiler save_name.lprof
如何对line_profiler排序? 由官网学习:line-profiler · PyPI
版本1-基于line_profiler 分析每行代码时间-排序
排序代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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 |
完整实例代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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中可能被统计两遍。
类似代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)