簡體   English   中英

time.clock 和 time.strftime 給出完全不同的結果

[英]time.clock and time.strftime Give Totally Different Results

我的程序每個循環進行一次測量並休眠指定的時間,在本例中為 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 始終給出完全虛假的廢話。 有沒有什么東西會導致這兩個函數給出完全無意義的結果?

編輯:為簡潔起見完全重寫帖子。

編輯:好吧,我有一個解決方案,但我不明白為什么。 This page says that time.clock() 在 Windows 下使用時給出掛鍾時間,但在 Unix 下使用時給出 CPU 時間。后來刪除他帖子的回答者之一,錯誤地說 time.clock() 是 CPU-時間,但我想知道盡管有文檔,他是否可能是正確的。 當我將所有對 time.clock() 的調用替換為對 time.time() 的調用時,我的程序現在甚至可以在有問題的計算機上運行。

考慮到任何應用程序的睡眠或定時器功能不正確,解決此問題的最佳方法是使用時間戳和目標時間來執行。

因此,將目標時間設置為 EPOCH + ExecuteTime,將間隔設置為較小的數字但不要太小以避免不需要的循環。 然后你檢查執行時間的循環部分是如何檢查它。

您知道應用程序什么時候會掛起甚至一毫秒嗎? 它不會根據啟動的實際時間驗證超時等,它只是相信它會從中斷的地方繼續。

行為不端的 PC 是否具有多核 CPU? 在 3.2 之前,可能會出現這樣一種情況,即一個線程被設置為一個核心,會釋放 GIL,而在分配給另一個核心的另一個線程可以獲得 GIL 之前,第一個線程已經重新獲取了它。 有關更多詳細信息,請參閱此帖子

更新:

更多的研究表明time.clocktime.time不一定使用相同的時鍾來測量時間。 正如您所發現的,解決方案是選擇一個或另一個,然后只使用那個。 您需要進行一些測試,看看哪個提供最好的穩定性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM