Python中的高精度时钟

92 投票
15 回答
174663 浏览
提问于 2025-04-15 17:14

有没有办法在Python中高精度地测量时间——比一秒还要精确?我怀疑有没有跨平台的方法可以做到这一点;我对Unix系统上的高精度时间感兴趣,特别是在运行在Sun SPARC机器上的Solaris系统。

timeit 似乎可以进行高精度的时间测量,但我想直接获取时间值,而不是测量一段代码执行的时间。

15 个回答

27

David的帖子是想展示Windows上的时钟分辨率。我对他的输出有些困惑,所以我写了一些代码,显示在我的Windows 8 x64笔记本上,time.time()的分辨率是1毫秒:

# measure the smallest time delta by spinning until the time changes
def measure():
    t0 = time.time()
    t1 = t0
    while t1 == t0:
        t1 = time.time()
    return (t0, t1, t1-t0)

samples = [measure() for i in range(10)]

for s in samples:
    print s

这个代码的输出是:

(1390455900.085, 1390455900.086, 0.0009999275207519531)
(1390455900.086, 1390455900.087, 0.0009999275207519531)
(1390455900.087, 1390455900.088, 0.0010001659393310547)
(1390455900.088, 1390455900.089, 0.0009999275207519531)
(1390455900.089, 1390455900.09, 0.0009999275207519531)
(1390455900.09, 1390455900.091, 0.0010001659393310547)
(1390455900.091, 1390455900.092, 0.0009999275207519531)
(1390455900.092, 1390455900.093, 0.0009999275207519531)
(1390455900.093, 1390455900.094, 0.0010001659393310547)
(1390455900.094, 1390455900.095, 0.0009999275207519531)

还有一种方法可以对时间间隔做1000次取平均:

reduce( lambda a,b:a+b, [measure()[2] for i in range(1000)], 0.0) / 1000.0

在连续运行两次时的输出是:

0.001
0.0010009999275207519

所以在我的Windows 8 x64上,time.time()的分辨率是1毫秒。

类似地,运行time.clock()的结果显示分辨率是0.4微秒:

def measure_clock():
    t0 = time.clock()
    t1 = time.clock()
    while t1 == t0:
        t1 = time.clock()
    return (t0, t1, t1-t0)

reduce( lambda a,b:a+b, [measure_clock()[2] for i in range(1000000)] )/1000000.0

返回结果是:

4.3571334791658954e-07

这大约是0.4e-06

有趣的是,time.clock()返回的是自第一次调用这个方法以来的时间,所以如果你想要微秒级的墙面时间,可以这样做:

class HighPrecisionWallTime():
    def __init__(self,):
        self._wall_time_0 = time.time()
        self._clock_0 = time.clock()

    def sample(self,):
        dc = time.clock()-self._clock_0
        return self._wall_time_0 + dc

(这可能会随着时间的推移而漂移,但你可以偶尔进行修正,比如dc > 3600可以每小时修正一次)

27

Python会尽力使用你所在平台上最精确的时间函数来实现time.time()这个功能:

/* Implement floattime() for various platforms */

static double
floattime(void)
{
    /* There are three ways to get the time:
      (1) gettimeofday() -- resolution in microseconds
      (2) ftime() -- resolution in milliseconds
      (3) time() -- resolution in seconds
      In all cases the return value is a float in seconds.
      Since on some systems (e.g. SCO ODT 3.0) gettimeofday() may
      fail, so we fall back on ftime() or time().
      Note: clock resolution does not imply clock accuracy! */
#ifdef HAVE_GETTIMEOFDAY
    {
        struct timeval t;
#ifdef GETTIMEOFDAY_NO_TZ
        if (gettimeofday(&t) == 0)
            return (double)t.tv_sec + t.tv_usec*0.000001;
#else /* !GETTIMEOFDAY_NO_TZ */
        if (gettimeofday(&t, (struct timezone *)NULL) == 0)
            return (double)t.tv_sec + t.tv_usec*0.000001;
#endif /* !GETTIMEOFDAY_NO_TZ */
    }

#endif /* !HAVE_GETTIMEOFDAY */
    {
#if defined(HAVE_FTIME)
        struct timeb t;
        ftime(&t);
        return (double)t.time + (double)t.millitm * (double)0.001;
#else /* !HAVE_FTIME */
        time_t secs;
        time(&secs);
        return (double)secs;
#endif /* !HAVE_FTIME */
    }
}

(来自 http://svn.python.org/view/python/trunk/Modules/timemodule.c?revision=81756&view=markup )

109

标准的 time.time() 函数可以提供亚秒级的时间精度,不过这个精度在不同的平台上会有所不同。在Linux和Mac上,它的精度大约是 +- 1 微秒,也就是0.001毫秒。而在Windows上,如果你使用的是Python 3.7之前的版本,由于时钟实现的问题,精度大约是 +- 16 毫秒,这主要是因为进程中断导致的。如果你想测量代码执行的时间,timeit 模块可以提供更高的精度。

>>> import time
>>> time.time()        #return seconds from epoch
1261367718.971009      

Python 3.7引入了一些新的函数到 time 模块,这些函数可以在较长的时间段内提供更高的精度:

>>> import time
>>> time.time_ns()
1530228533161016309
>>> time.time_ns() / (10 ** 9) # convert to floating-point seconds
1530228544.0792289

撰写回答