我的程序每个循环测量一次并睡眠指定的量,在这种情况下为10秒。它还衡量时间。它以两种方式衡量时间。一次使用time.strftime,另一次使用time.clock()。在我的大多数计算机上,这些结果完全一致,我没有任何问题。在一台计算机上,他们根本不同意。
这是有问题的代码段。在我的程序中,它在三个线程中独立运行。 self.delay是一个包含用户指定延迟的浮点数,在本例中为10.0秒。
cycles = 0
startTime = time.clock()
while(blah)
cycleBeginTime = time.clock()
...
t = time.strftime("%Y-%m-%d %H:%M:%S")
...
cycles += 1
cycleEndTime = time.clock()
wakeUp = startTime + cycles * self.delay
if cycleEndTime > wakeUp: #we overslept
continue
else:
#not guaranteed to sleep for the exact specified amount of time
time.sleep(float(wakeUp - cycleEndTime))
afterSleepTime = time.clock()
print ("sleep(" + str(wakeUp - cycleEndTime) + ") lasted " +
str(afterSleepTime - cycleEndTime) +" seconds\n" +
"Total time for this cycle: " +
str(afterSleepTime - cycleBeginTime) +
"\ntime from start of cycle to sleep " +
str(cycleEndTime-cycleBeginTime) )
以下是使用time.clock测量的控制台上的结果。跳到下一部分以获取摘要。
sleep(9.8107975515) lasted 4.31354512806 seconds
Total time for this cycle: 4.50274184753
time from start of cycle to sleep 0.189196719463
sleep(9.83803382537) lasted 4.35964227608 seconds
Total time for this cycle: 4.5216022856
time from start of cycle to sleep 0.161960009523
sleep(9.83973893539) lasted 4.36409510551 seconds
Total time for this cycle: 4.52435043356
time from start of cycle to sleep 0.160255328054
sleep(15.3537603228) lasted 5.42625884166 seconds
Total time for this cycle: 5.56417636459
time from start of cycle to sleep 0.137917522931
sleep(15.3879203849) lasted 5.45131225502 seconds
Total time for this cycle: 5.5384287755
time from start of cycle to sleep 0.0871165204752
sleep(15.3801304296) lasted 5.45686671345 seconds
Total time for this cycle: 5.55443364994
time from start of cycle to sleep 0.0975669364944
sleep(19.7024141096) lasted 2.5903386547 seconds
Total time for this cycle: 2.81485116786
time from start of cycle to sleep 0.224512513157
sleep(19.7236584582) lasted 2.61606277881 seconds
Total time for this cycle: 2.81505236078
time from start of cycle to sleep 0.198989581976
sleep(19.7569903213) lasted 2.64424758408 seconds
Total time for this cycle: 2.8228942459
time from start of cycle to sleep 0.178646661821
sleep(26.8608515814) lasted 3.1923968974 seconds
Total time for this cycle: 3.44044448649
time from start of cycle to sleep 0.248047589093
sleep(26.9264651571) lasted 3.24803654453 seconds
Total time for this cycle: 3.42464766929
time from start of cycle to sleep 0.176611124756
sleep(26.9123819307) lasted 6.19344847627 seconds
Total time for this cycle: 6.39064386998
time from start of cycle to sleep 0.197195393715
sleep(30.50445713) lasted 11.3544706882 seconds
Total time for this cycle: 11.5452852063
time from start of cycle to sleep 0.190814518069
sleep(30.5479180492) lasted 11.4011029222 seconds
Total time for this cycle: 11.5583578442
time from start of cycle to sleep 0.157254922059
sleep(30.5384771841) lasted 11.3943939803 seconds
Total time for this cycle: 11.5739287254
time from start of cycle to sleep 0.179534745126
sleep(29.032023896) lasted 9.57638019147 seconds
Total time for this cycle: 9.6907935091
time from start of cycle to sleep 0.114413317628
sleep(28.9997437096) lasted 9.55454254173 seconds
Total time for this cycle: 9.70431450053
time from start of cycle to sleep 0.149771958799
sleep(29.0315669415) lasted 9.57838381284 seconds
Total time for this cycle: 9.69044695504
time from start of cycle to sleep 0.112063142198
sleep(29.2684610421) lasted 11.5343751591 seconds
Total time for this cycle: 11.7100907949
time from start of cycle to sleep 0.175715635808
sleep(29.4380200767) lasted 11.7063676658 seconds
Total time for this cycle: 11.7231073229
time from start of cycle to sleep 0.01673965716
sleep(29.2840066914) lasted 11.5395576362 seconds
Total time for this cycle: 11.7081641622
time from start of cycle to sleep 0.168606525989
以下是time.strftime的时间戳摘要,与time.clock和尝试的睡眠时间的测量结果进行比较。
2012-04-04 17:22:07
2012-04-04 17:22:17 (diff 10s. Attempted sleep time 10s time.clock says 4.5s)
2012-04-04 17:22:32 (diff 15s. Attempted sleep time 15.4s time.clock says 5.4s)
2012-04-04 17:22:52 (diff 20s. Attempted sleep time 19.7s time.clock says 2.8s)
2012-04-04 17:23:46 (diff 54s. Attempted sleep time 27s time.clock says 3.4s)
2012-04-04 17:24:16 (diff 30s. Attempted sleep time 30.5s time.clock says 11.5s)
2012-04-04 17:24:45 (diff 29s. Attempted sleep time 29s time.clock says 9.7s)
2012-04-04 17:25:15 (diff 30s. Attempted sleep time 29.4s time.clock says 11.7s)
正如你所看到的,time.strftime最同意睡眠,但不是所有的时间(他们在2012-04-04 17:23:46不同意),而time.clock一直给出完全虚假的废话。有什么东西可以导致这两个函数给出完全无意义的结果吗?
编辑:为了简洁,完全改写了帖子。
编辑:嗯,我有一个解决方案,但我不明白为什么。 This page表示time.clock()在Windows下使用时会给出挂钟时间,但在Unix下使用会占用CPU时间。其中一个已经删除帖子的回答者,错误地说time.clock()是CPU时间,但我不知道他是否可能是正确的,尽管有文档。当我通过调用time.time()将所有调用替换为time.clock()时,我的程序现在甚至可以在问题计算机上运行。答案 0 :(得分:0)
考虑到任何应用程序睡眠或计时器功能不正确,处理此问题的最佳方法是使用时间戳和目标时间来执行。
因此,将目标时间设置为等于EPOCH + ExecuteTime,将间隔设置为较小的数字但不能太小以避免不必要的周期。然后你检查执行时间的部分是如何检查它。
您知道应用程序何时挂起甚至毫秒?它不会根据启动的实际时间来验证超时等,它只是认为它从中断的地方继续进行。
答案 1 :(得分:0)
行为不端的PC是否有多核cpu?在3.2之前,有可能得到一个线程被设置为一个核心,将释放GIL,并且在分配给另一个核心的另一个线程可以获得GIL之前第一个线程重新获取它的情况。有关详细信息,请参阅this post。
更新
更多研究表明,time.clock
和time.time
不一定使用相同的时钟来衡量时间。正如您所发现的,解决方案是选择一个或另一个,然后使用那个。你需要做一些测试,看看哪个提供了最好的稳定性。