中断时间

中断时间是自系统上次启动以来的时长(以 100 纳秒为单位)。 系统启动时,中断时间从零开始计数,并在每个时钟中断时按时钟周期的长度递增。 时钟周期的确切长度取决于基础硬件,可能因系统而异。

系统时间不同,中断时间计数不会被用户或 Windows 时间服务调整,因此成为了测量短持续时间的更好选择。 需要比中断时间计数更高的精度的应用程序应使用高分辨率计时器。 使用 QueryPerformanceFrequency 函数检索高分辨率计时器的频率,并使用 QueryPerformanceCounter 函数检索计数器的值。

QueryInterruptTimeQueryInterruptTimePreciseQueryUnbiasedInterruptTimeQueryUnbiasedInterruptTimePrecise 函数可用于检索中断时间计数。 无偏差中断时间意味着仅计算系统处于工作状态的时间,因此,中断时间计数不会“偏差”系统在睡眠或休眠中花费的时间。

Windows Server 2008、Windows Vista、Windows Server 2003 和 Windows XP/2000:从 Windows 7 和 Windows Server 2008 R2 开始,QueryUnbiasedInterruptTime 函数可用。

timeBeginPeriodtimeEndPeriod 函数设置的计时器分辨率会影响 QueryInterruptTimeQueryUnbiasedInterruptTime 函数的解析。 然而,不建议增加计时器分辨率,因为它会降低整体系统性能,并通过阻止处理器进入省电状态来增加功耗。 相反,应用程序应使用高分辨率计时器。

注意

QueryInterruptTimeQueryInterruptTimePreciseQueryUnbiasedInterruptTimeQueryUnbiasedInterruptTimePrecise 函数在 Windows 的调试(“已检查”)版本中会产生不同的结果,因为中断时间计数和周期计数提前了大约 49 天。 这有助于识别系统运行很长时间后才会出现的 bug。

 

以下示例演示了如何通过调用 QueryInterruptTimeQueryInterruptTimePreciseQueryUnbiasedInterruptTimeQueryUnbiasedInterruptTimePrecise 函数来检索中断时间计数。 当您构建调用这些函数的控制台应用程序时,链接到 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);
}

若要检索睡眠或休眠占用的已用时间,请使用 GetTickCountGetTickCount64 函数,或使用系统运行时间性能计数器。 可以从注册表项 HKEY_PERFORMANCE_DATA 内的性能数据中检索此性能计数器。 返回的值是一个 8 字节值。 有关详细信息,请参阅 性能计时器

QueryInterruptTime

QueryInterruptTimePrecise

QueryUnbiasedInterruptTime

QueryUnbiasedInterruptTimePrecise

timeBeginPeriod

timeEndPeriod