c++利用windows函數(shù)實(shí)現(xiàn)計(jì)時(shí)示例
//Windows系統(tǒng)下可以用 time(),clock(),timeGetTime(),GetTickCount(),QueryPerformanceCounter()來(lái)對(duì)一段程序代碼進(jìn)行計(jì)時(shí)
#include <stdio.h>
#include <windows.h>
#include <time.h> //time_t time() clock_t clock()
#include <Mmsystem.h> //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("%d\n", timeEnd - timeBegin);
//用clock()來(lái)計(jì)時(shí),以毫秒為單位
clock_t clockBegin, clockEnd;
clockBegin = clock();
Sleep(800);
clockEnd = clock();
printf("%d\n", clockEnd - clockBegin);
//用timeGetTime()來(lái)計(jì)時(shí),以毫秒為單位
DWORD dwBegin, dwEnd;
dwBegin = timeGetTime();
Sleep(800);
dwEnd = timeGetTime();
printf("%d\n", dwEnd - dwBegin);
//用GetTickCount()來(lái)計(jì)時(shí),以毫秒為單位
DWORD dwGTCBegin, dwGTCEnd;
dwGTCBegin = GetTickCount();
Sleep(800);
dwGTCEnd = GetTickCount();
printf("%d\n", 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í)器頻率%lf\n", dff);
printf("第一次計(jì)時(shí)器值%I64d\n第二次計(jì)時(shí)器值%I64d\n計(jì)時(shí)器差%I64d\n", c1, c2, c2 - c1);
printf("計(jì)時(shí)%lf毫秒\n\n", (c2 - c1) * 1000 / dff);
return 0;
}

- C++ clock()解析如何使用時(shí)鐘計(jì)時(shí)的應(yīng)用
- 總結(jié)UNIX/LINUX下C++程序計(jì)時(shí)的方法
- C++獲取當(dāng)前系統(tǒng)時(shí)間的方法總結(jié)
- C++中獲取UTC時(shí)間精確到微秒的實(shí)現(xiàn)代碼
- C++指針作為函數(shù)的參數(shù)進(jìn)行傳遞時(shí)需要注意的一些問(wèn)題
- C#調(diào)用C++版本dll時(shí)的類(lèi)型轉(zhuǎn)換需要注意的問(wèn)題小結(jié)
- C++函數(shù)返回值為對(duì)象時(shí),構(gòu)造析構(gòu)函數(shù)的執(zhí)行細(xì)節(jié)
- C++設(shè)置系統(tǒng)時(shí)間及系統(tǒng)時(shí)間網(wǎng)絡(luò)更新的方法
- C++如何實(shí)現(xiàn)簡(jiǎn)單的計(jì)時(shí)器詳解
相關(guān)文章
從零學(xué)習(xí)構(gòu)造系統(tǒng)之bazel示例詳解
這篇文章主要為大家介紹了從零學(xué)習(xí)構(gòu)造系統(tǒng)之bazel示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02詳解C++中的內(nèi)聯(lián)函數(shù)和函數(shù)重載
這篇文章主要介紹了詳解C++中的內(nèi)聯(lián)函數(shù)和函數(shù)重載,是C++入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09C++設(shè)計(jì)模式之簡(jiǎn)單工廠模式的實(shí)現(xiàn)示例
這篇文章主要給大家介紹了關(guān)于C++設(shè)計(jì)模式之簡(jiǎn)單工廠模式的相關(guān)資料,簡(jiǎn)單工廠模式,主要用于創(chuàng)建對(duì)象,添加類(lèi)時(shí),不會(huì)影響以前的系統(tǒng)代碼,需要的朋友可以參考下2021-06-06