GetTickCount() 函數的作用和用法


原文: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) 應用

用來計算某個操作所使用的時間:   

  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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM