后浪云Python教程:怎么计算python程序运行时间

python脚本使用统计时间的方式是time.clock(),而这种方式统计的是CPU的执行时间,不是程序的执行时间。
下面我们就来看一下计算python程序运行时间的方法:
def time_function(f, *args): """ Call a function f with args and return the time (in seconds) that it took to execute. """ import time tic = time.time() f(*args) toc = time.time() return toc - tic
使用time()返回程序运行时和程序结束的时间戳,然后将两个时间相减即可获得程序运行时间。
版权声明:
作者:后浪云
链接:https://idc.net/help/182389/
文章版权归作者所有,未经允许请勿转载。
THE END
