- 相關(guān)推薦
c++利用windows函數(shù)實(shí)現(xiàn)計(jì)時(shí)范例
計(jì)時(shí)怎樣利用代碼實(shí)現(xiàn)呢?以下是為大家分享的c++利用windows函數(shù)實(shí)現(xiàn)計(jì)時(shí)范例,供大家參考借鑒,歡迎瀏覽!
復(fù)制代碼 代碼如下:
//Windows系統(tǒng)下可以用 time(),clock(),timeGetTime(),GetTickCount(),QueryPerformanceCounter()來(lái)對(duì)一段程序代碼進(jìn)行計(jì)時(shí)
#include
#include
#include//time_t time() clock_t clock()
#include//timeGetTime()
#pragma comment(lib, "Winmm.lib") //timeGetTime()
//使用方法:將Sleep()函數(shù)換成需要測(cè)試運(yùn)行時(shí)間的函數(shù)即可。
int main()
{ //用time()來(lái)計(jì)時(shí),以秒為單位
time_t timeBegin, timeEnd;
timeBegin = time(NULL);
Sleep(1000);
timeEnd = time(NULL);
printf("%dn", timeEnd - timeBegin);
//用clock()來(lái)計(jì)時(shí),以毫秒為單位
clock_t clockBegin, clockEnd;
clockBegin = clock();
Sleep(800);
clockEnd = clock();
printf("%dn", clockEnd - clockBegin);
//用timeGetTime()來(lái)計(jì)時(shí),以毫秒為單位
DWORD dwBegin, dwEnd;
dwBegin = timeGetTime();
Sleep(800);
dwEnd = timeGetTime();
printf("%dn", dwEnd - dwBegin);
//用GetTickCount()來(lái)計(jì)時(shí),以毫秒為單位
DWORD dwGTCBegin, dwGTCEnd;
dwGTCBegin = GetTickCount();
Sleep(800);
dwGTCEnd = GetTickCount();
printf("%dn", dwGTCEnd - dwGTCBegin);
//用QueryPerformanceCounter()來(lái)計(jì)時(shí),以微秒為單位
LARGE_INTEGER large_interger;
double dff;
__int64 c1, c2;
QueryPerformanceFrequency(&large_interger);
dff = large_interger.QuadPart;
QueryPerformanceCounter(&large_interger);
c1 = large_interger.QuadPart;
Sleep(800);
QueryPerformanceCounter(&large_interger);
c2 = large_interger.QuadPart;
printf("本機(jī)高精度計(jì)時(shí)器頻率%lfn", dff);
printf("第一次計(jì)時(shí)器值%I64dn第二次計(jì)時(shí)器值%I64dn計(jì)時(shí)器差%I64dn", c1, c2, c2 - c1);
printf("計(jì)時(shí)%lf毫秒nn", (c2 - c1) * 1000 / dff);
return 0;
}
【c++利用windows函數(shù)實(shí)現(xiàn)計(jì)時(shí)】相關(guān)文章:
c和c++中實(shí)現(xiàn)函數(shù)回調(diào)的方法08-30
C++函數(shù)模板09-14
C++函數(shù)考點(diǎn)歸納09-30
c++函數(shù)指針使用示例07-26
C++如何調(diào)用matlab函數(shù)06-29
C++函數(shù)指針學(xué)習(xí)教程10-01