怎么计算python程序运行时间

图片[1]-怎么计算python程序运行时间-uusu优素-乐高,模型,3d打印,编程

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

下面我们就来看一下计算python程序运行时间的方法:

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

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

原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容