在Python中有没有一种方法可以高精度地测量时间--比一秒还要精确? 我怀疑是否有跨平台的方式来实现这一点; 我对Unix上的高精度时间感兴趣,特别是在Sun SPARC机器上运行的Solaris。
timeit似乎能够进行高精度的时间测量,但我不想测量代码段需要多长时间,而是直接访问时间值。
标准的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
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)
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)
还有一种方法,对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
将每小时更正一次)