GetTickCount() 用法

GetTickCount函数用于获取系统启动以来经过的时间,单位为毫秒。在Release配置下,返回值是从设备启动开始的时间,不包括系统暂停的时间。在Debug配置下,会从启动时间中减去180秒来检测溢出情况。此函数不适合用于需要高精度时间的应用。

GetTickCount记录了从系统启动时经过的时间,精确到毫秒。

For Release configurations, this function returns the number of milliseconds since the device booted, excluding any time that the system was suspended. GetTickCount starts at 0 on boot and then counts up from there.

For Debug configurations, 180 seconds is subtracted from the the number of milliseconds since the device booted. This allows code that uses GetTickCount to be easily tested for correct overflow handling.

Syntax
 
DWORD GetTickCount(void);

Return Value
The number of milliseconds indicates success.

Remarks
The resolution of the system timer is based on the OEM's setting. Check with the OEM for details.

The elapsed time is stored as a DWORD value. Therefore, the time will rollover to zero if the system is run continuously for 49.7 days.

For Debug configurations, 180 seconds is subtracted to check for overflow conditions in code that relies on GetTickCount. If this code started within 3 minutes of the device booting, it will experience an overflow condition if it runs for a certain amount of time.

Note:
 
You should not use GetTickCount for drift sensitive applications. 

When using GetTickCount, subtraction is safe, even if the rollover occurred, and subtraction always yields the correct difference and the number of clock ticks passed between two tick values. Comparing tick values directly does not always yield the correct results; only compare the differences. Be sure that your code can service the difference before the second rollover, that is, before another 49.7 days pass. Comparisons such as the following are not safe.

   
#define DELTA_TICKS sample_tick_value

// initialized somewhere in the code
DWORD dwStartTick = GetTickCount();
DWORD dwEndTick =   GetTickCount() + DELTA_TICKS;

// The following function fails on a rollover.
BOOL    no_compare_tick_difference()
{
  if ( GetTickCount() > dwEndTick )
    return ( TRUE);
  return (FALSE);
}

The following code shows how to properly use GetTickCount by comparing tick differences. This code handles the rollover situation.

   
BOOL compare_tick_difference()
{
  if ( (GetTickCount() – dwStartTick) > DELTA_TICKS)
    return ( TRUE);
  return (FALSE);
}

You can use the GetTickCount function to time the duration of an activity as shown in the example below.

   
dwOldTime = GetTickCount();
DoSomething();
dwTimeElapsed = GetTickCount() – dwOldTime;

On the STMicro platform, the time returned by GetTickCount includes a ~0.014% time drift, which is by design. This translates to a time lag of approximately 1 second every 2 hours.

Requirements
OS Versions: Windows CE 1.0 and later.

Header: Winbase.h.

Link Library: Coredll.lib.

 

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/jmunic/archive/2009/08/29/4496954.aspx

<think> 嗯,用户询问的是Windows 11系统中使用GetTickCount的方法。根据提供的参考资料,GetTickCount是一个记录系统启动后毫秒数的API,但存在49.7天回绕问题。 需要注意引用[1]和[3]提到GetTickCount的32位限制会导致计时器在49.7天后归零。这对需要长期运行的应用程序来说是个隐患。 引用[2]和[4]提到解决方案是使用GetTickCount64,这个64位版本能避免回绕问题。即使Windows 11兼容旧API,新开发也强烈推荐使用64位版本。 用户明确要求GetTickCount,所以需要同时说明基本用法和限制事项。准备在回复中包括: 1. 基础用法示例代码 2. 32位限制的详细警告 3. 替代方案GetTickCount64的强烈推荐 4. 实际应用时的注意事项 考虑在代码示例中加入误差处理,比如时间差为负值时的回绕检测逻辑。最后建议关键应用优先选用64位API。 </think> 在 Windows 11 系统中使用 `GetTickCount` 的方法与旧版 Windows 相同,但需特别注意其**32位回绕问题**(约49.7天重置),以下是详细步骤和注意事项: --- ### **1. 基础用法** ```cpp #include <Windows.h> // 必需头文件 #include <iostream> int main() { DWORD start = GetTickCount(); // 记录起始时间点 // 模拟耗时操作(例如:延时1秒) Sleep(1000); DWORD end = GetTickCount(); // 记录结束时间点 DWORD elapsed = end - start; // 计算时间差(毫秒) std::cout << "耗时: " << elapsed << " 毫秒" << std::endl; return 0; } ``` - **关键点**: - `GetTickCount()` 返回 `DWORD` (32位无符号整数),表示系统启动后的毫秒数。 - 时间差计算:`end - start`(结果可能为负值需处理回绕,见下文)。 --- ### **2. 回绕问题处理** 由于返回值约 **49.7天**($2^{32}/1000/60/60/24 \approx 49.7$)会归零[^1][^3],长时间计时需特殊处理: ```cpp DWORD CalculateDelta(DWORD newer, DWORD older) { // 处理回绕:若 newer < older 说明发生归零 if (newer >= older) { return newer - older; } else { // 最大值(0xFFFFFFFF)减去旧值,再加新值 return (0xFFFFFFFF - older) + newer + 1; } } ``` --- ### **3. 更优替代方案:`GetTickCount64()`** Windows 11 强烈推荐使用 **64位版本**,彻底避免回绕问题[^2][^4]: ```cpp #include <Windows.h> #include <iostream> int main() { ULONGLONG start = GetTickCount64(); // 64位无符号整数 Sleep(1000); // 延时操作 ULONGLONG end = GetTickCount64(); ULONGLONG elapsed = end - start; // 直接计算无回绕风险 std::cout << "耗时: " << elapsed << " 毫秒" << std::endl; return 0; } ``` - **优势**: - 返回值是 `ULONGLONG` (64位),需 **5.8亿年** 才会回绕[^4]。 - 无需额外处理溢出逻辑。 --- ### **注意事项** 1. **精度限制**: - 实际精度约 10-16 毫秒(取决于硬件时钟中断频率)。 2. **系统休眠影响**: - 系统休眠/睡眠期间计时暂停[^2]。 3. **多线程安全**: - 函数本身线程安全,但需自行保证 `start/end` 变量的原子性。 4. **适用场景**: - 短周期计时(如性能分析)。 - **长期运行程序务必使用 `GetTickCount64`**[^2][^4]。 > 📌 **总结**:在 Windows 11 中,优先选择 `GetTickCount64()`;若需兼容旧代码使用 `GetTickCount()`,必须实现回绕处理逻辑。 --- ### 相关问题 1. `GetTickCount` 和 `QueryPerformanceCounter` 在精度上有何区别? 2. 如何在跨平台代码中实现高精度计时? 3. Windows 系统时间函数(如 `timeGetTime`)与 `GetTickCount` 有何异同? 4. 回绕处理函数在极端情况下(如连续运行超过100天)是否仍可靠? [^1]: 引用自“windows c++ (6) GetTickCount长时间计时问题” [^2]: 引用自“GetTickCount64 用法举例” [^3]: 引用自“GetTickCount函数” [^4]: 引用自“Windows API GetTickCount64() 和 _kbhit()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值