【分享】 Windows 各類計時函數總結

本文對Windows平臺下經常使用的計時函數進行總結,包括精度爲秒、毫秒、微秒三種精度的 5種方法。分爲在標準C/C++下的二種time()及clock(),標準C/C++因此使用的time()及clock()不只能夠用在 Windows系統,也能夠用於Linux系統。在Windows系統下三種,使用Windows提供的API接口timeGetTime()、 GetTickCount()及QueryPerformanceCounter()來完成。文章最後給出了5種計時方法示例代碼。html

 

標準C/C++的二個計時函數time()及clock()windows

 

time_t time(time_t *timer);函數

返回以格林尼治時間(GMT)爲標準,從1970年1月1日00:00:00到如今的此時此刻所通過的秒數。學習

time_t實際是個long長整型typedef long time_t;測試

頭文件:#include <time.h>spa

 

clock_t clock(void);orm

返回進程啓動到調用函數時所通過的CPU時鐘計時單元(clock tick)數,在MSDN中稱之爲掛鐘時間(wal-clock),以毫秒爲單位。htm

clock_t實際是個long長整型typedef long clock_t;接口

頭文件:#include <time.h>進程

 

 

Windows系統API函數

timeGetTime()、GetTickCount()及QueryPerformanceCounter()

 

DWORD timeGetTime(VOID);

返回系統時間,以毫秒爲單位。系統時間是從系統啓動到調用函數時所通過的毫秒數。注意,這個值是32位的,會在0到2^32之間循環,約49.71天。

頭文件:#include <Mmsystem.h>            

引用庫:#pragma comment(lib, "Winmm.lib")  

 

DWORD WINAPI GetTickCount(void);

這個函數和timeGetTime()同樣也是返回系統時間,以毫秒爲單位。

頭文件:直接使用#include <windows.h>就能夠了。

 

高精度計時,以微秒爲單位(1毫秒=1000微秒)。

先看二個函數的定義

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);

獲得高精度計時器的值(若是存在這樣的計時器)。

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

返回硬件支持的高精度計數器的頻率(次每秒),返回0表示失敗。

再看看LARGE_INTEGER

它實際上是一個聯合體,能夠獲得__int64 QuadPart;也能夠分別獲得低32位DWORD LowPart和高32位的值LONG HighPart。

在使用時,先使用QueryPerformanceFrequency()獲得計數器的頻率,再計算二次調用QueryPerformanceCounter()所得的計時器值之差,用差去除以頻率就獲得精確的計時了。

頭文件:直接使用#include <windows.h>就能夠了。

 

 

下面給出示例代碼,能夠在你電腦上測試下。

[cpp] view

  1. //Windows系統下time(),clock(),timeGetTime(),GetTickCount(),QueryPerformanceCounter()來計時 by MoreWindows  

  2. #include <stdio.h>  

  3. #include <windows.h>  

  4. #include <time.h> //time_t time()  clock_t clock()  

  5. #include <Mmsystem.h>             //timeGetTime()  

  6. #pragma comment(lib, "Winmm.lib")   //timeGetTime()  

  7.   

  8. int main()  

  9. {  

  10.     //用time()來計時  秒  

  11.     time_t timeBegin, timeEnd;  

  12.     timeBegin = time(NULL);  

  13.     Sleep(1000);  

  14.     timeEnd = time(NULL);  

  15.     printf("%d\n", timeEnd - timeBegin);  

  16.       

  17.       

  18.     //用clock()來計時  毫秒  

  19.     clock_t  clockBegin, clockEnd;  

  20.     clockBegin = clock();  

  21.     Sleep(800);  

  22.     clockEnd = clock();  

  23.     printf("%d\n", clockEnd - clockBegin);  

  24.       

  25.       

  26.     //用timeGetTime()來計時  毫秒  

  27.     DWORD  dwBegin, dwEnd;  

  28.     dwBegin = timeGetTime();  

  29.     Sleep(800);  

  30.     dwEnd = timeGetTime();  

  31.     printf("%d\n", dwEnd - dwBegin);  

  32.       

  33.       

  34.     //用GetTickCount()來計時  毫秒  

  35.     DWORD  dwGTCBegin, dwGTCEnd;  

  36.     dwGTCBegin = GetTickCount();  

  37.     Sleep(800);  

  38.     dwGTCEnd = GetTickCount();  

  39.     printf("%d\n", dwGTCEnd - dwGTCBegin);  

  40.       

  41.       

  42.     //用QueryPerformanceCounter()來計時  微秒  

  43.     LARGE_INTEGER  large_interger;  

  44.     double dff;  

  45.     __int64  c1, c2;  

  46.     QueryPerformanceFrequency(&large_interger);  

  47.     dff = large_interger.QuadPart;  

  48.     QueryPerformanceCounter(&large_interger);  

  49.     c1 = large_interger.QuadPart;  

  50.     Sleep(800);  

  51.     QueryPerformanceCounter(&large_interger);  

  52.     c2 = large_interger.QuadPart;  

  53.     printf("本機高精度計時器頻率%lf\n", dff);  

  54.     printf("第一次計時器值%I64d 第二次計時器值%I64d 計時器差%I64d\n", c1, c2, c2 - c1);  

  55.     printf("計時%lf毫秒\n", (c2 - c1) * 1000 / dff);  

  56.       

  57.     printf("By MoreWindows\n");  

  58.     return 0;  

  59. }                                                                                                                                                                                                     Windows技術分享與學習:http://edu.51cto.com/course/courseList/id-54.html

相關文章
相關標籤/搜索