溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C/C++各種計(jì)時(shí)函數(shù)總結(jié)是怎樣的

發(fā)布時(shí)間:2021-10-14 10:34:47 來源:億速云 閱讀:148 作者:柒染 欄目:編程語言

這篇文章給大家介紹C/C++各種計(jì)時(shí)函數(shù)總結(jié)是怎樣的,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

Windows平臺(tái)下常用的計(jì)時(shí)函數(shù)總結(jié),包括精度為秒、毫秒、微秒三種精度的5 種方法。分為在標(biāo)準(zhǔn)C/C++下的二種time()及clock(),標(biāo)準(zhǔn)C/C++所以使用的time()及clock()不僅可以用在Windows 系統(tǒng),也可以用于Linux系統(tǒng)。在Windows系統(tǒng)下三種,使用Windows提供的API接口timeGetTime()、 GetTickCount()及QueryPerformanceCounter()來完成。文章最后給出了5種計(jì)時(shí)方法示例代碼。

標(biāo)準(zhǔn)C/C++的二個(gè)計(jì)時(shí)函數(shù)time()及clock()

time_t time(time_t *timer);

返回以格林尼治時(shí)間(GMT)為標(biāo)準(zhǔn),從1970年1月1日00:00:00到現(xiàn)在的此時(shí)此刻所經(jīng)過的秒數(shù)。

time_t實(shí)際是個(gè)long長整型typedef long time_t;

頭文件:#include <time.h>

clock_t clock(void);

返回進(jìn)程啟動(dòng)到調(diào)用函數(shù)時(shí)所經(jīng)過的CPU時(shí)鐘計(jì)時(shí)單元(clock tick)數(shù),在MSDN中稱之為掛鐘時(shí)間(wal-clock),以毫秒為單位。

clock_t實(shí)際是個(gè)long長整型typedef long clock_t;

頭文件:#include <time.h>

Windows系統(tǒng)API函數(shù)

timeGetTime()、GetTickCount()及QueryPerformanceCounter()

DWORD timeGetTime(VOID);

返回系統(tǒng)時(shí)間,以毫秒為單位。系統(tǒng)時(shí)間是從系統(tǒng)啟動(dòng)到調(diào)用函數(shù)時(shí)所經(jīng)過的毫秒數(shù)。注意,這個(gè)值是32位的,會(huì)在0到2^32之間循環(huán),約49.71天。

頭文件:#include <Mmsystem.h>            

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

DWORD WINAPI GetTickCount(void);

這個(gè)函數(shù)和timeGetTime()一樣也是返回系統(tǒng)時(shí)間,以毫秒為單位。

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

高精度計(jì)時(shí),以微秒為單位(1毫秒=1000微秒)。

先看二個(gè)函數(shù)的定義

BOOL QueryPerformanceCounter(LARGE_INTEGER *lpPerformanceCount);

得到高精度計(jì)時(shí)器的值(如果存在這樣的計(jì)時(shí)器)。

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

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

再看看LARGE_INTEGER

它其實(shí)是一個(gè)聯(lián)合體,可以得到__int64 QuadPart;也可以分別得到低32位DWORD LowPart和高32位的值LONG HighPart。

在使用時(shí),先使用QueryPerformanceFrequency()得到計(jì)數(shù)器的頻率,再計(jì)算二次調(diào)用QueryPerformanceCounter()所得的計(jì)時(shí)器值之差,用差去除以頻率就得到精確的計(jì)時(shí)了。

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

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

view plain

  1. //Windows系統(tǒng)下time(),clock(),timeGetTime(),GetTickCount(),QueryPerformanceCounter()來計(jì)時(shí) 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. int main()  

  8. {  

  9.     //用time()來計(jì)時(shí)  秒

  10.     time_t timeBegin, timeEnd;  

  11.     timeBegin = time(NULL);  

  12.     Sleep(1000);  

  13.     timeEnd = time(NULL);  

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



  15.     //用clock()來計(jì)時(shí)  毫秒

  16.     clock_t  clockBegin, clockEnd;  

  17.     clockBegin = clock();  

  18.     Sleep(800);  

  19.     clockEnd = clock();  

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



  21.     //用timeGetTime()來計(jì)時(shí)  毫秒

  22.     DWORD  dwBegin, dwEnd;  

  23.     dwBegin = timeGetTime();  

  24.     Sleep(800);  

  25.     dwEnd = timeGetTime();  

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



  27.     //用GetTickCount()來計(jì)時(shí)  毫秒

  28.     DWORD  dwGTCBegin, dwGTCEnd;  

  29.     dwGTCBegin = GetTickCount();  

  30.     Sleep(800);  

  31.     dwGTCEnd = GetTickCount();  

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



  33.     //用QueryPerformanceCounter()來計(jì)時(shí)  微秒

  34.     LARGE_INTEGER  large_interger;  

  35.     double dff;  

  36.     __int64  c1, c2;  

  37.     QueryPerformanceFrequency(&large_interger);  

  38.     dff = large_interger.QuadPart;  

  39.     QueryPerformanceCounter(&large_interger);  

  40.     c1 = large_interger.QuadPart;  

  41.     Sleep(800);  

  42.     QueryPerformanceCounter(&large_interger);  

  43.     c2 = large_interger.QuadPart;  

  44.     printf("本機(jī)高精度計(jì)時(shí)器頻率%lf\n", dff);  

  45.     printf("第一次計(jì)時(shí)器值%I64d 第二次計(jì)時(shí)器值%I64d 計(jì)時(shí)器差%I64d\n", c1, c2, c2 - c1);  

  46.     printf("計(jì)時(shí)%lf毫秒\n", (c2 - c1) * 1000 / dff);  


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

  48.     return 0;  

  49. }  


關(guān)于C/C++各種計(jì)時(shí)函數(shù)總結(jié)是怎樣的就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI