---
title: "GetTickCount() 函数的作用和用法(转)"
author: "J2EE大鸟"
date: "2013-06-09T15:56:26Z"
tags: []
categories: ["编程语言"]
slug: "gettickcount"
draft: false
lastmod: "2013-06-09T15:56:26Z"
id: 1884925
url: "https://benworld.iteye.com/blog/1884925"
---

**转自：http://www.cnblogs.com/jxsoft/archive/2011/10/17/2215366.html**

**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\) 应用

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

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  Start:=GetTickCount;     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   ...//执行耗时的操作     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   Stop:=GetTickCount;     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)   TimeUsed:=(Stop-Start)/1000;     //使用了xxx秒

用来定时：  

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)void main()   
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif){  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  DWORD dwLast;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  DWORD dwCurrent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  DWORD dwInterval = 1000;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  dwLast = GetTickCount();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  int i = 0;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  while(true)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)     {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        dwCurrent = GetTickCount();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if( dwCurrent - dwLast &lt; dwInterval )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        continue;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         //your code to be executed when interval is elapsed  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        printf("dwLast,dwCurrent,diff:%d,%d,%d ",dwLast,dwCurrent,dwCurrent-dwLast);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         //your code to determine when to break  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        if( i &gt; 10 ) break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        i++;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        dwLast = dwCurrent;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        printf("Time is up!");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)        break;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   getchar();     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)   return;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)} 

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

以上引用：

[http://www.wesoho.com/article.asp?id=2072](http://www.wesoho.com/article.asp?id=2072)

[http://bitboy.blog.edu.cn/user1/19986/archives/2005/1001846.shtml](http://bitboy.blog.edu.cn/user1/19986/archives/2005/1001846.shtml)