在Python中处理时间之clock()方法的使用_技术学院_宜昌市隼壹珍商贸有限公司

您好,欢迎访问宜昌市隼壹珍商贸有限公司

400 890 5375
当前位置: 主页 > 新闻动态 > 技术学院

在Python中处理时间之clock()方法的使用

发布时间:2026-01-18  |  点击率:

 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),因为该程序开始的时间。

全国统一服务电话

400 890 5375

电子邮箱:879577@qq.com

公司地址:宜昌市西陵区黄河路5号三峡明珠10栋1051室

咨询微信

TEL:13680874598