发布于 2015-06-21 10:27:49 | 196 次阅读 | 评论: 0 | 来源: 网络整理

clock()方法返回当前的处理器时间,以秒表示Unix上一个浮点数。精度取决于具有相同名称的C函数,但在任何情况下,这是使用于基准Python或定时的算法函数。

在Windows中该函数返回,因为这个函数的第一个调用过去挂钟秒钟,作为浮点数,基于Win32函数QueryPerformanceCounter。

语法

以下是clock()方法的语法:


time.clock()

参数

  • NA

返回值

此方法返回当前处理器时间作为浮点数在UNIX秒钟,并在Windows中表示返回这个函数的第一个调用过去挂钟秒钟,作为浮点数。

例子

下面的例子显示了clock()方法的使用。


#!/usr/bin/python
import time

def procedure():
    time.sleep(2.5)

# measure process time
t0 = time.clock()
procedure()
print time.clock() - t0, "seconds process time"

# measure wall time
t0 = time.time()
procedure()
print time.time() - t0, "seconds wall time"

当我们运行上面的程序,它会产生以下结果:


0.0 seconds process time
2.50023603439 seconds wall time

注:并非所有的系统可以测量真实的处理时间。在这样的系统(包括Windows),因为该程序开始的时间。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务