在Python中处理时间之clock()方法的使用 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),因为该程序开始的时间。