time.time() drift over repeated calls
每按一次键就得到一个时间戳,如下所示:
1 2 3 4 5 | init_timestamp = time.time() while (True): c = getch() offset = time.time() - init_timestamp print("%s,%s" % (c,offset), file=f) |
(
我正在验证我实际键入密钥的录音时间戳。在将第一个时间戳与波形对齐之后,随后的时间戳略微漂移但一致。我的意思是,保存的时间戳晚于按键波形,随着时间的推移越来越晚。
我有理由相信波形定时是正确的(即记录不是快或慢),因为在记录中我还包括一个非常准确的时钟滴答,它与第二个标记完美排列。
我知道
为什么我会看到这种漂移的行为,我该怎么做才能避免它?
刚刚使用
gettimeofday() and time() should only be used to get the current time if the current wall-clock time is actually what you want. They should never be used to measure time or schedule an event X time into the future.
You usually aren't running NTP on your wristwatch, so it probably won't jump a second or two (or 15 minutes) in a random direction because it happened to sync up against a proper clock at that point. Good NTP implementations try to not make the time jump like this. They instead make the clock go faster or slower so that it will drift to the correct time. But while it's drifting you either have a clock that's going too fast or too slow. It's not measuring the passage of time properly.
(链接)。 所以基本上测量
根据您使用的操作系统,您需要使用
对于Windows操作系统,你将需要使用
对于posix系统(linux,osx),你应该使用
在您的代码中添加以下内容,使您的应用程序更加跨系统兼容。
1 2 3 4 5 6 7 8 9 10 11 12 13 | import os if os.name == 'posix': from time import time as get_time else: from time import clock as get_time # now use get_time() to return the timestamp init_timestamp = get_time() while (True): c = getch() offset = get_time() - init_timestamp print("%s,%s" % (c,offset), file=f) ... |