停機時間
停機時間 是系統上次啟動後的時間量,以 100 奈秒間隔為單位。 當系統啟動時,插斷時間計數會從零開始,並在每一個時鐘中斷時以時鐘刻度長度遞增。 時鐘刻度的實際長度取決於基礎硬體,而且在系統之間可能會有所不同。
不同于 系統時間,停機時間計數不會受限於使用者或 Windows 時間服務的調整,因此更適合用來測量短期持續時間。 需要比插斷時間計數更高的精確度的應用程式應該使用 高解析度計時器。 使用 QueryPerformanceFrequency 函式來擷取高解析度計時器和 QueryPerformanceCounter 函 式的頻率,以擷取計數器的值。
QueryInterruptTime、QueryInterruptTimePrecise、QueryUnbiasedInterruptTime和QueryUnbiasedInterruptTimePrecise函式可用來擷取停機時間計數。 無偏差的停機時間表示只有系統處於工作狀態的時間才會計算,因此,系統花費在睡眠或休眠的時間不會「偏差」停機時間計數。
Windows Server 2008、Windows Vista、Windows Server 2003 和 Windows XP/2000: 從 Windows 7 和 Windows Server 2008 R2 開始,即可使用 QueryUnbiasedInterruptTime 函式。
timeBeginPeriod和timeEndPeriod函式所設定的計時器解析度會影響QueryInterruptTime和QueryUnbiasedInterruptTime函式的解析度。 不過,不建議增加計時器解析度,因為它可以降低整體系統效能,並防止處理器進入省電狀態來增加耗電量。 相反地,應用程式應該使用高解析度計時器。
注意
QueryInterruptTime、QueryInterruptTimePrecise、QueryUnbiasedInterruptTime 和 QueryUnbiasedInterruptTimePrecise函式會在 Windows 的偵錯 (「核取」) 組建上產生不同的結果,因為停機時間計數和刻度計數大約會進階 49 天。 這有助於識別在系統長時間執行之前可能不會發生的 Bug。 已核取的組建可透過 Microsoft Developer Network (MSDN) 網站提供給 MSDN 訂閱者使用。
下列範例示範如何藉由呼叫QueryInterruptTime、QueryInterruptTimePrecise、QueryUnbiasedInterruptTime 和 QueryUnbiasedInterruptTimePrecise函式來擷取停機時間計數。 當您建置呼叫這些函式的主控台應用程式時,OneCore.lib 程式庫的連結。
#include "stdafx.h"
#include <windows.h>
#include <realtimeapiset.h>
void InterruptTimeTest()
{
ULONGLONG InterruptTime;
ULONGLONG PreciseInterruptTime;
ULONGLONG UnbiasedInterruptTime;
ULONGLONG PreciseUnbiasedInterruptTime;
// The interrupt time that QueryInterruptTime reports is based on the
// latest tick of the system clock timer. The system clock timer is
// the hardware timer that periodically generates interrupts for the
// system clock. The uniform period between system clock timer
// interrupts is referred to as a system clock tick, and is typically
// in the range of 0.5 milliseconds to 15.625 milliseconds, depending
// on the hardware platform. The interrupt time value retrieved by
// QueryInterruptTime is accurate within a system clock tick.
QueryInterruptTime(&InterruptTime);
printf("Interrupt time: %.7f seconds\n",
(double)InterruptTime/(double)10000000);
// Precise interrupt time is more precise than the interrupt time that
// QueryInterruptTime reports because the functions that report
// precise interrupt time read the timer hardware directly.
QueryInterruptTimePrecise(&PreciseInterruptTime);
printf("Precise interrupt time: %.7f seconds\n",
(double)PreciseInterruptTime/(double)10000000);
// Unbiased interrupt time means that only time that the system is in
// the working state is counted. Therefore, the interrupt-time count
// is not biased by time the system spends in sleep or hibernation.
QueryUnbiasedInterruptTime(&UnbiasedInterruptTime);
printf("Unbiased interrupt time: %.7f seconds\n",
(double)UnbiasedInterruptTime/(double)10000000);
// QueryUnbiasedInterruptTimePrecise gets an interrupt-time count
// that is both unbiased and precise, as defined in the comments
// included earlier in this example.
QueryUnbiasedInterruptTimePrecise(&PreciseUnbiasedInterruptTime);
printf("Precise unbiased interrupt time: %.7f seconds\n",
(double)PreciseUnbiasedInterruptTime/(double)10000000);
}
int main(void)
{
void InterruptTimeTime();
InterruptTimeTest();
return(0);
}
若要擷取考慮睡眠或休眠的經過時間,請使用 GetTickCount 或 GetTickCount64 函式,或使用系統啟動時間效能計數器。 此效能計數器可以從登錄機碼中的效能資料 擷取HKEY_PERFORMANCE_DATA。 傳回的值是 8 位元組值。 如需相關資訊,請參閱 Performance Counters。
相關主題
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應