Python中的高精度时钟

2024-05-23 22:04:48 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python中,有没有一种高精度测量时间的方法——比一秒钟更精确?我怀疑有这样一种跨平台的方式,我对UNIX上的高精度时间很感兴趣,特别是在Sun SPARC机器上运行的Solaris。

timeit似乎能够进行高精度的时间测量,但我希望直接访问时间值,而不是测量代码片段所需的时间。


Tags: 方法代码机器方式时间跨平台unixsun
3条回答

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

大卫的帖子试图显示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)

还有一种方法,对delta进行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将每小时更正一次)

标准的time.time()函数提供亚秒精度,尽管精度因平台而异。对于Linux和Mac,精度为+-1微秒或0.001毫秒。由于进程中断导致的时钟实现问题,Windows上的Python使用了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

相关问题 更多 >