(网摘)10种检测Python程序运行时间、CPU和内存占用的方法
此文原始版本转自互联网,本文作者进行代码验证后,略有删改
代码验证环境如下
因此,在这篇文章中我将介绍7个不同的Python工具,来检查代码中函数的执行时间以及内存和CPU的使用。
1. 使用装饰器来衡量函数执行时间
有一个简单方法,那就是定义一个装饰器来测量函数的执行时间,并输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import time from functools import wraps def fn_timer(function): @wraps (function) def function_timer( * args, * * kwargs): t0 = time.time() result = function( * args, * * kwargs) t1 = time.time() print ( "Total time running %s: %s seconds" % (function.func_name, str (t1 - t0)) ) return result return function_timer |
接着,将这个装饰器添加到需要测量的函数之前,如下所示:
1 2 3 | @fn_timer def myfunction(...): ... |
例如,这里检测一个函数排序含有200万个随机数字的数组所需的时间:
1 2 3 4 5 6 | @fn_timer def random_sort(n): return sorted ([random.random() for i in range (n)]) if __name__ = = "__main__" : random_sort( 2000000 ) |
执行脚本时,会看到下面的结果:
1 | Total time running random_sort: 1.41124916077 seconds |
1.2
使用<装饰器
范例
2
执行脚本时,会看到下面的结果:
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import time from functools import wraps template_db = { "node_templates" : { "Forwarding_path1" : { "type" : "tosca.nodes.nfv.FP.Tacker" , "description" : "creates path (CP12->CP22)" , "properties" : { "policy" : { "type" : "ACL" , "criteria" : [ { "network_src_port_id" : { "get_input" : "network_src_port_id" }}, { "destination_port_range" : "80-1024" }, { "ip_proto" : 6 }, { "ip_dst_prefix" : "10.10.0.100/24" } ] }, "path" : [ { "capability" : { "get_input" : "capability" }, "forwarder" : "VNFFG_VNFD1" }, { "capability" : "CP22" , "forwarder" : "VNFFG_VNFD2" } ], "id" : 51 } } }, "description" : { "get_input" : "description" }, "groups" : { "VNFFG1" : { "type" : "tosca.groups.nfv.VNFFG" , "description" : "HTTP to Corporate Net" , "members" : [ "Forwarding_path1" ], "properties" : { "vendor" : "tacker" , "connection_point" : [ "CP12" , "CP22" ], "version" : 1.0 , "constituent_vnfs" : [ "VNFFG_VNFD1" , "VNFFG_VNFD2" ], "number_of_endpoints" : 5 , "dependent_virtual_link" : [ "VL12" , "VL22" ] } } } } def fn_timer(function): @wraps (function) def function_timer( * args, * * kwargs): t0 = time.time() result = function( * args, * * kwargs) t1 = time.time() print ( "Total time running %s: %s seconds" % (function.func_name, str (t1 - t0))) return result return function_timer @fn_timer def dict_matched001(src_dict, target_dict): for param_key in src_dict.keys(): if target_dict.get(param_key) is None : return @fn_timer def dict_matched002(src_dict, target_dict): unmatch = set (src_dict) - set (target_dict) if unmatch: return g_src_dict = template_db g_target_dict = template_db def main(): print ( "Start to check dict_matched001 running time." ) dict_matched001(g_src_dict, g_target_dict) print ( "Start to check dict_matched002 running time." ) dict_matched002(g_src_dict, g_target_dict) if __name__ = = "__main__" : main() |
执行脚本时,会看到下面的结果:
1 2 3 4 | Start to check dict_matched001 running time. Total time running dict_matched001: 5.96046447754e - 06 seconds Start to check dict_matched002 running time. Total time running dict_matched002: 1.4066696167e - 05 seconds |
2. 使用timeit模块
另一种方法是使用timeit模块,用来计算平均时间消耗。
执行下面的脚本可以运行该模块。
1 | python - m timeit - n 4 - r 5 - s "import timing_functions" "timing_functions.random_sort(2000000)" |
这里的timing_functions是Python脚本文件名称。
在输出的末尾,可以看到以下结果:
1 | 4 loops, best of 5 : 2.08 sec per loop |
这表示测试了4次,平均每次测试重复5次,最好的测试结果是2.08秒。
如果不指定测试或重复次数,默认值为10次测试,每次重复5次。
3. 使用Unix系统中的time命令
然而,装饰器和timeit都是基于Python的。在外部环境测试Python时,unix time实用工具就非常有用。
运行time实用工具:
1 | $ time - p python timing_functions.py |
输出结果为:
1 2 3 4 | Total time running random_sort: 1.3931210041 seconds real 1.49 user 1.40 sys 0.08 |
第一行来自预定义的装饰器,其他三行为:
- real表示的是执行脚本的总时间
- user表示的是执行脚本消耗的CPU时间。
- sys表示的是执行内核函数消耗的时间。
注意:根据维基百科的定义,内核是一个计算机程序,用来管理软件的输入输出,并将其翻译成CPU和其他计算机中的电子设备能够执行的数据处理指令。
因此,Real执行时间和User+Sys执行时间的差就是消耗在输入/输出和系统执行其他任务时消耗的时间。
4. 使用cProfile模块
如果想知道每个函数和方法消耗了多少时间,以及这些函数被调用了多少次,可以使用cProfile模块。
1 | $ python - m cProfile - s cumulative timing_functions.py |
现在可以看到代码中函数的详细描述,其中含有每个函数调用的次数,由于使用了-s选项(累加),最终结果会根据每个函数的累计执行时间排序。
读者会发现执行脚本所需的总时间比以前要多。这是由于测量每个函数的执行时间这个操作本身也是需要时间。
5. 使用line_profiler模块
line_profiler模块可以给出执行每行代码所需占用的CPU时间。
首先,安装该模块:
1 | $ pip install line_profiler |
接着,需要指定用@profile检测哪个函数(不需要在代码中用import导入模块):
1 2 3 4 5 6 7 8 | @profile def random_sort2(n): l = [random.random() for i in range (n)] l.sort() return l if __name__ = = "__main__" : random_sort2( 2000000 ) |
最好,可以通过下面的命令获得关于random_sort2函数的逐行描述。
1 | $ kernprof - l - v timing_functions.py |
其中-l表示逐行解释,-v表示表示输出详细结果。通过这种方法,我们看到构建数组消耗了44%的计算时间,而sort()方法消耗了剩余的56%的时间。
同样,由于需要检测执行时间,脚本的执行时间更长了。
6. 使用memory_profiler模块
memory_profiler模块用来基于逐行测量代码的内存使用。使用这个模块会让代码运行的更慢。
安装方法如下:
1 | pip install memory_profiler |
另外,建议安装psutil包,这样memory_profile会运行的快一点:
1 | $ pip install psutil |
与line_profiler相似,使用@profile装饰器来标识需要追踪的函数。接着,输入:
1 | $ python - m memory_profiler timing_functions.py |
脚本的执行时间比以前长1或2秒。如果没有安装psutil包,也许会更长。
从结果可以看出,内存使用是以MiB为单位衡量的,表示的mebibyte(1MiB = 1.05MB)。
7. 使用guppy包
最后,通过这个包可以知道在代码执行的每个阶段中,每种类型(str、tuple、dict等)分别创建了多少对象。
安装方法如下:
1 | $ pip install guppy |
接着,将其添加到代码中:
1 2 3 4 5 6 7 8 9 10 11 12 | from guppy import hpy def random_sort3(n): hp = hpy() print "Heap at the beginning of the functionn" , hp.heap() l = [random.random() for i in range (n)] l.sort() print "Heap at the end of the functionn" , hp.heap() return l if __name__ = = "__main__" : random_sort3( 2000000 ) |
运行代码:
1 | $ python timing_functions.py |
可以看到输出结果为:
通过在代码中将heap()放置在不同的位置,可以了解到脚本中的对象创建和删除操作的流程。
如果想学习更多关于Python代码速度优化方面的知识,有时间可以去看看这本书
《High Performance Python: Practical Performant Programming for Humans, september 2014.》
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架