GetTickCount() 函数的作用和用法

DWORD GetTickCount(void);    

1) 定义

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.

在Release版本中,该函数从0开始计时,返回自设备启动后的毫秒数(不含系统暂停时间)。

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.

在Debug版本中,设备启动后便从计时器中减去180秒。这样方便测试使用该函数的代码的正确溢出处理。

Return Values

The number of milliseconds indicates success.

返回值:如正确,返回毫秒数。

Header: Winbase.h.
Link Library: Coredll.lib.

2) 应用

用来计算某个操作所使用的时间:  

  Start:=GetTickCount;   
   ...//执行耗时的操作   
   Stop:=GetTickCount;   
   TimeUsed:=(Stop-Start)/1000;     //使用了xxx秒

用来定时:  

复制代码
void main() 
{
  DWORD dwLast;
  DWORD dwCurrent;
  DWORD dwInterval = 1000;
  dwLast = GetTickCount();
  int i = 0;
  while(true)
     {
        dwCurrent = GetTickCount();
        if( dwCurrent - dwLast < dwInterval )
        continue;
         //your code to be executed when interval is elapsed
        printf("dwLast,dwCurrent,diff:%d,%d,%d ",dwLast,dwCurrent,dwCurrent-dwLast);
         //your code to determine when to break
        if( i > 10 ) break;
        i++;
        dwLast = dwCurrent;
        printf("Time is up!");
        break;
     }
   getchar();   
   return;
} 
复制代码

对于一般的实时控制,使用GetTickCount()函数就可以满足精度要求,但要进一步提高计时精度,就要采用QueryPerformanceFrequency()函数和QueryPerformanceCounter()函数。这两个函数是VC提供的仅供Windows   9X使用的高精度时间函数,并要求计算机从硬件上支持高精度计时器。

以上引用:

http://www.wesoho.com/article.asp?id=2072

http://bitboy.blog.edu.cn/user1/19986/archives/2005/1001846.shtml

posted on   癫狂编程  阅读(4096)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2017-09-07 Delphi.XE2破解方法

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
好的代码像粥一样,都是用时间熬出来的
点击右上角即可分享
微信分享提示