中斷時間

中斷時間 是系統上次啟動後的時間量,以 100 奈秒間隔為單位。 當系統啟動時,插斷時間計數會從零開始,並在每個時鐘中斷時遞增時鐘刻度長度。 時鐘刻度的確切長度取決於基礎硬體,而且可能會因系統而異。

不同於 系統時間,中斷時間計數不受使用者或 Windows 時間服務的調整,因此更適合用來測量短持續時間。 需要比插斷時間計數更高的精確度的應用程式應該使用 高解析度定時器。 使用 QueryPerformanceFrequency 函式來擷取高解析度定時器和 QueryPerformanceCounter 函式的頻率,以擷取計數器的值。

QueryInterruptTime、QueryInterruptTimePreciseQueryUnbiasedInterruptTime QueryUnbiasedInterruptTimePrecise 函式可用來擷取中斷時間計數。 不偏不倚的中斷時間表示只有系統處於工作狀態的時間才會計算,因此,系統在睡眠或休眠時,中斷時間計數不會「偏向」。

Windows Server 2008、Windows Vista、Windows Server 2003 和 Windows XP/2000:從 Windows 7 和 Windows Server 2008 R2 開始,即可使用 QueryUnbiasedInterruptTime 函式。

timeBeginPeriod 和 timeEndPeriod 函式所設定的定時器解析度會影響 QueryInterruptTime 和 QueryUnbiasedInterruptTime 函式的解析度。 不過,不建議增加定時器解析度,因為它可以降低整體系統效能,並防止處理器進入省電狀態來增加耗電量。 相反地,應用程式應該使用高解析度定時器。

注意

QueryInterruptTime、QueryInterruptTimePreciseQueryUnbiasedInterruptTime 和 QueryUnbiasedInterruptTimePrecise 函式會在 Windows 的偵錯 (“checked”) 組建上產生不同的結果,因為中斷時間計數和刻度計數大約會進階 49 天。 這有助於識別在系統長時間執行之前可能不會發生的 Bug。

 

下列範例示範如何藉由呼叫 QueryInterruptTime、QueryInterruptTimePreciseQueryUnbiasedInterruptTimeQueryUnbiasedInterruptTimePrecise 函式來擷取中斷時間計數。 當您建置呼叫這些函式的控制台應用程式時,連結到 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 函式,或使用 System Up Time 性能計數器。 您可以從登錄機碼 中的效能數據擷取此性能計數器HKEY_PERFORMANCE_DATA。 傳回的值是8位元組值。 如需相關資訊,請參閱 Performance Counters

QueryInterruptTime

QueryInterruptTimePrecise

QueryUnbiasedInterruptTime

QueryUnbiasedInterruptTimePrecise

timeBeginPeriod

timeEndPeriod