当前位置: 首页 > 图灵资讯 > 行业资讯> 怎么计算python程序运行时间

怎么计算python程序运行时间

来源:图灵python
时间: 2026-05-05 10:01:59

使用python脚本统计时间的方法是timee.clock(),这样统计CPU的执行时间,而不是程序的执行时间。

让我们来看看计算python程序运行时间的方法:

deftime_function(f,*args):
"""
Callafunctionfwithargsandreturnthetime(inseconds)thatittooktoexecute.
"""
importtime
tic=time.time()
f(*args)
toc=time.time()
returntoc-tic

使用time()返回程序运行时和程序结束时的时间戳,然后减少两个时间以获得程序运行时间。

请关注Python视频教程栏目,了解更多Python知识。