我的程序在每个循环中进行一次测量并休眠指定的时间,在本例中为 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 最同意 sleep ,但不是所有时间(他们在 2012-04-04 17:23:46 不同意),而 time.clock 一直完全是胡说八道。有什么东西会导致这两个函数给出完全无意义的结果吗?
编辑:为简洁起见,完全重写了帖子。
编辑:嗯,我有一个解决方案,但我不明白为什么。 这个页面说 time.clock() 在 Windows 下使用时给出挂钟时间,但在 Unix 下使用时给出 CPU 时间。此后删除他帖子的回答者之一错误地说 time.clock() 是 CPU 时间,但我想知道他是否可能是正确的,尽管有文档。当我将所有对 time.clock() 的调用替换为对 time.time() 的调用时,我的程序现在甚至可以在有问题的计算机上运行。