您现在的位置是:课程教程文章

怎么计算python程序运行时间

2023-12-18 19:03课程教程文章 人已围观

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()返回程序运行时和程序结束的时间戳,然后将两个时间相减即可获得程序运行时间。

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

课程教程:怎么计算python程序运行时间

上一篇:怎么判断python的版本号

下一篇:没有了

站点信息

  • 文章统计篇文章