在Python中使用哪个更好?Time.clock()还是time.time()?哪一种更准确?

例如:

start = time.clock()
... do something
elapsed = (time.clock() - start)

vs.

start = time.time()
... do something
elapsed = (time.time() - start)

这取决于你在乎什么。如果您指的是WALL TIME(即墙上时钟上的时间),则TIME .clock()不能提供精度,因为它可能管理CPU时间。


Clock() ->浮点数

返回CPU时间或进程启动后的实时时间 第一次调用clock()。这和系统的精度一样高 记录。

Time() ->浮点数

返回当前时间(以秒为单位)。 如果系统时钟提供,可能会出现几分之一秒。

通常time()更精确,因为操作系统存储进程运行时间的精度与存储系统时间(即实际时间)的精度不同。


简单的答案是:大多数时候time.clock()会更好。 然而,如果你正在为某些硬件计时(例如你在GPU中放入的某些算法),那么time.clock()将摆脱这个时间,而time.time()是唯一剩下的解决方案。

注意:无论使用何种方法,计时将取决于您无法控制的因素(进程何时切换,频率如何,……),这对于time.time()来说更糟糕,但对于time.clock()也存在,因此您永远不应该只运行一个计时测试,而是始终运行一系列测试并查看时间的均值/方差。


简单回答:在Python中使用time.clock()进行计时。

在*nix系统上,clock()返回处理器时间为浮点数,以秒表示。在Windows上,它以浮点数的形式返回自第一次调用此函数以来所经过的秒数。

time()返回自纪元以来的秒数,以UTC为单位,作为浮点数。不能保证您将获得比1秒更好的精度(即使time()返回一个浮点数)。还要注意,如果在两次调用该函数之间设置了系统时钟,那么第二次函数调用将返回一个较低的值。


从3.3开始,time.clock()已弃用,建议使用time.process_time()或time.perf_counter()。

在2.7之前,根据time模块docs:

time.clock() On Unix, return the current processor time as a floating point number expressed in seconds. The precision, and in fact the very definition of the meaning of “processor time”, depends on that of the C function of the same name, but in any case, this is the function to use for benchmarking Python or timing algorithms. On Windows, this function returns wall-clock seconds elapsed since the first call to this function, as a floating point number, based on the Win32 function QueryPerformanceCounter(). The resolution is typically better than one microsecond.

此外,还有timeit模块用于对代码段进行基准测试。


其他人回答了re: time.time() vs. time.clock()。

但是,如果您是为了基准测试/分析目的而对代码块的执行进行计时,则应该查看timeit模块。


在Unix上,time.clock()测量当前进程已使用的CPU时间量,因此它不适用于测量从过去某个点开始的流逝时间。在Windows上,它将测量自第一次调用函数以来所经过的时钟秒数。在任意一个系统上,time.time()将返回自epoch以来经过的秒数。

如果您正在编写仅用于Windows的代码,则两者都可以工作(尽管您将使用不同的方法- time.clock()不需要减法)。如果要在Unix系统上运行,或者您想要保证可移植的代码,则需要使用time.time()。


这种差异是平台特有的。

例如,clock()在Windows上与Linux上有很大不同。

对于您描述的这类示例,您可能希望使用"timeit"模块。


据我所知,time.clock()具有系统所允许的最大精度。


有一件事要记住: 修改系统时间会影响time.time(),但不会影响time.clock()。

我需要控制一些自动测试的执行。如果测试用例的一个步骤所花费的时间超过了给定的时间量,那么该TC就会中止以继续进行下一个步骤。

但是有时需要一个步骤来更改系统时间(检查被测试应用程序的调度器模块),因此在几个小时后设置系统时间后,TC超时,测试用例被终止。我必须从time.time()切换到time.clock()来正确处理这个问题。


在Linux上,time()比clock()具有更好的精度。Clock()的精度小于10毫秒。而time()提供完美的精度。 我的测试用的是CentOS 6.4和python 2.6

using time():

1 requests, response time: 14.1749382019 ms
2 requests, response time: 8.01301002502 ms
3 requests, response time: 8.01491737366 ms
4 requests, response time: 8.41021537781 ms
5 requests, response time: 8.38804244995 ms

使用时钟():

1 requests, response time: 10.0 ms
2 requests, response time: 0.0 ms 
3 requests, response time: 0.0 ms
4 requests, response time: 10.0 ms
5 requests, response time: 0.0 ms 
6 requests, response time: 0.0 ms
7 requests, response time: 0.0 ms 
8 requests, response time: 0.0 ms

对比Ubuntu Linux和Windows 7的测试结果。

在Ubuntu上

>>> start = time.time(); time.sleep(0.5); (time.time() - start)
0.5005500316619873

Windows 7操作系统

>>> start = time.time(); time.sleep(0.5); (time.time() - start)
0.5

我使用这段代码来比较2种方法。我的操作系统是windows 8,处理器核心i5, RAM 4GB

import time

def t_time():
    start=time.time()
    time.sleep(0.1)
    return (time.time()-start)


def t_clock():
    start=time.clock()
    time.sleep(0.1)
    return (time.clock()-start)




counter_time=0
counter_clock=0

for i in range(1,100):
    counter_time += t_time()

    for i in range(1,100):
        counter_clock += t_clock()

print "time() =",counter_time/100
print "clock() =",counter_clock/100

输出:

time() = 0.0993799996376

clock() = 0.0993572257367

正确答案:它们都是相同长度的分数。

但如果主题是时间,哪个更快?

一个小测试案例:

import timeit
import time

clock_list = []
time_list = []

test1 = """
def test(v=time.clock()):
    s = time.clock() - v
"""

test2 = """
def test(v=time.time()):
    s = time.time() - v
"""
def test_it(Range) :
    for i in range(Range) :
        clk = timeit.timeit(test1, number=10000)
        clock_list.append(clk)
        tml = timeit.timeit(test2, number=10000)
        time_list.append(tml)

test_it(100)

print "Clock Min: %f Max: %f Average: %f" %(min(clock_list), max(clock_list), sum(clock_list)/float(len(clock_list)))
print "Time  Min: %f Max: %f Average: %f" %(min(time_list), max(time_list), sum(time_list)/float(len(time_list)))

我不是在瑞士实验室工作,但我做过测试。

基于这个问题:time.clock()比time.time()更好

编辑:time.clock()是内部计数器,所以不能在外部使用,得到限制最大32位浮点数,如果不存储第一个/最后一个值,就不能继续计数。不能合并另一个计数器…


正如其他人所注意到的,time.clock()已被弃用,取而代之的是time.perf_counter()或time.process_time(),但Python 3.7通过time.perf_counter_ns()、time.process_time_ns()和time.time_ns()以及其他3个函数引入了纳秒分辨率计时。

PEP 564中详细介绍了这6个新的纳秒分辨率函数:

time.clock_gettime_ns (clock_id) 时间。clock_settime_ns (clock_id、时间:int) time.monotonic_ns () time.perf_counter_ns () time.process_time_ns () time.time_ns () 这些函数类似于没有_ns后缀的版本,但是 返回一个纳秒数作为Python int。

正如其他人也注意到的那样,使用timeit模块为函数和小代码段计时。


time.clock()在Python 3.8中被移除,因为它具有平台相关的行为:

在Unix上,返回以秒表示的浮点数形式的当前处理器时间。 在Windows上,此函数以浮点数的形式返回自第一次调用该函数以来经过的时钟秒数 打印(time.clock ());time . sleep (10);print (time.clock ()) # Linux: 0.0382 0.0384 #参见处理器时间 # Windows: 26.1224 36.1566 #见clock Time

那么选择哪个函数呢?

Processor Time: This is how long this specific process spends actively being executed on the CPU. Sleep, waiting for a web request, or time when only other processes are executed will not contribute to this. Use time.process_time() Wall-Clock Time: This refers to how much time has passed "on a clock hanging on the wall", i.e. outside real time. Use time.perf_counter() time.time() also measures wall-clock time but can be reset, so you could go back in time time.monotonic() cannot be reset (monotonic = only goes forward) but has lower precision than time.perf_counter()