中断时间

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

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

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

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

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

注意

QueryInterruptTimeQueryInterruptTimePreciseQueryUnbiasedInterruptTimeQueryUnbiasedInterruptTimePrecise 函数在调试 (“checked”) Windows 版本中生成不同的结果,因为中断时间计数和时钟周期计数提前了大约 49 天。 这有助于识别在系统长时间运行之前可能不会发生的 bug。 选中的版本可通过 Microsoft 开发人员网络 (MSDN) 网站向 MSDN 订阅者提供。

 

以下示例演示如何通过调用 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);
}

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

QueryInterruptTime

QueryInterruptTimePrecise

QueryUnbiasedInterruptTime

QueryUnbiasedInterruptTimePrecise

timeBeginPeriod

timeEndPeriod