c++利用windows函數(shù)實現(xiàn)計時示例
//Windows系統(tǒng)下可以用 time(),clock(),timeGetTime(),GetTickCount(),QueryPerformanceCounter()來對一段程序代碼進行計時
#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ù)換成需要測試運行時間的函數(shù)即可。
int main()
{ //用time()來計時,以秒為單位
time_t timeBegin, timeEnd;
timeBegin = time(NULL);
Sleep(1000);
timeEnd = time(NULL);
printf("%d\n", timeEnd - timeBegin);
//用clock()來計時,以毫秒為單位
clock_t clockBegin, clockEnd;
clockBegin = clock();
Sleep(800);
clockEnd = clock();
printf("%d\n", clockEnd - clockBegin);
//用timeGetTime()來計時,以毫秒為單位
DWORD dwBegin, dwEnd;
dwBegin = timeGetTime();
Sleep(800);
dwEnd = timeGetTime();
printf("%d\n", dwEnd - dwBegin);
//用GetTickCount()來計時,以毫秒為單位
DWORD dwGTCBegin, dwGTCEnd;
dwGTCBegin = GetTickCount();
Sleep(800);
dwGTCEnd = GetTickCount();
printf("%d\n", dwGTCEnd - dwGTCBegin);
//用QueryPerformanceCounter()來計時,以微秒為單位
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("本機高精度計時器頻率%lf\n", dff);
printf("第一次計時器值%I64d\n第二次計時器值%I64d\n計時器差%I64d\n", c1, c2, c2 - c1);
printf("計時%lf毫秒\n\n", (c2 - c1) * 1000 / dff);
return 0;
}

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