1

我的程序在每个循环中进行一次测量并休眠指定的时间,在本例中为 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() 的调用时,我的程序现在甚至可以在有问题的计算机上运行。

4

2 回答 2

0

行为不端的 PC 是否具有多核 CPU?在 3.2 之前,可能会出现这样一种情况:一个线程被设置为一个内核,会释放 GIL,而在分配给另一个内核的另一个线程可以获得 GIL 之前,第一个线程已经重新获取它。有关更多详细信息,请参阅此帖子

更新:

再多一点研究表明,time.clock并不time.time一定使用同一个时钟来测量时间。正如您所发现的,解决方案是选择一个或另一个,然后只使用那个。您需要进行一些测试,看看哪个提供了最佳稳定性。

于 2012-04-05T01:28:08.593 回答
0

考虑到任何应用程序的睡眠或定时器功能不正确,解决此问题的最佳方法是使用时间戳和目标执行时间。

因此,将目标时间设置为 EPOCH + ExecuteTime,将间隔设置为一个较小的数字但不要太小以避免不必要的循环。然后你在循环中检查执行时间的部分是如何检查它。

您知道应用程序何时会挂起一毫秒吗?它不会根据启动的实际时间验证超时等,它只是相信它从中断的地方继续。

于 2012-04-05T00:00:47.580 回答