C++高精度計(jì)時(shí)的幾種方法總結(jié)(測(cè)試函數(shù)運(yùn)行時(shí)間)
一、clock()函數(shù)——毫妙級(jí)
C系統(tǒng)調(diào)用方法,所需頭文件ctime/time.h,即windows和linux都可以使用。
1、clock()返回類型為clock_t類型
2、clock_t實(shí)際為long 類型, typedef long clock_t
3、clock() 函數(shù),返回從 開啟這個(gè)程序進(jìn)程 到 程序中調(diào)用clock()函數(shù) 時(shí)之間的CPU時(shí)鐘計(jì)時(shí)單元(clock tick)數(shù)(掛鐘時(shí)間),返回單位是毫秒
4、可以用常量CLOCKS_PER_SEC, 這個(gè)常量表示每一秒(per second)有多少個(gè)時(shí)鐘計(jì)時(shí)單元
#include <time.h> //引入頭文件 void time1() { clock_t start, end; start = clock(); fun(); //需計(jì)時(shí)的函數(shù) end = clock(); cout << "elapsed time in clock = " << double(end - start) << "ms" << endl; }
二、GetTickCount()函數(shù)(精度16ms左右)——毫妙級(jí)
GetTickCount()是一個(gè)Windows API,所需頭文件為<windows.h>。是通過計(jì)算從函數(shù)開始運(yùn)行計(jì)時(shí),直到函數(shù)運(yùn)行結(jié)束所求出函數(shù)的運(yùn)行時(shí)間。它返回從操作系統(tǒng)啟動(dòng)所經(jīng)過的毫秒數(shù),
此處需要注意的是,這個(gè)函數(shù)所求的的運(yùn)行時(shí)間并非準(zhǔn)確運(yùn)行時(shí)間,不過相對(duì)來說比較準(zhǔn)確,它的精度和CPU有關(guān),一般精度在16ms左右,由于GetTickCount()返回值以32位的雙字類型DWORD存儲(chǔ),所以它的存儲(chǔ)最大值是(2^32-1) ms約為49.71天,一旦一個(gè)程序運(yùn)行時(shí)間超過這個(gè)值,這個(gè)數(shù)字就會(huì)歸為0。
#include <windows.h> //引入頭文件 void time2() { DWORD t1, t2; t1 = GetTickCount(); fun(); //需計(jì)時(shí)的函數(shù) t2 = GetTickCount(); cout << "elapsed time in GetTickCount = " << double(t2 - t1) << "ms" << endl; }
三、高精度時(shí)控函數(shù)QueryPerformanceCounter()——微妙級(jí)
原理:這里使用高精度時(shí)控函數(shù)QueryPerformanceFrequency(),QueryPerformanceCounter()
它們是兩個(gè)精度很高的函數(shù),精度單位為微秒。使用QueryPerformanceCounter()即可獲取這個(gè)高精度計(jì)時(shí)器的值,但是由于機(jī)器的原因,它們實(shí)際上的精度會(huì)大幅度受到機(jī)器運(yùn)作的影響,則必須向系統(tǒng)查詢它們確切的運(yùn)作頻率QueryPerformanceFrequency()函數(shù)提供了這個(gè)功能,可以通過這一個(gè)函數(shù)來獲取高精度計(jì)時(shí)器的運(yùn)作頻率(在一秒鐘之內(nèi)它的運(yùn)作次數(shù)),用兩次調(diào)用QueryPerformanceCounter()函數(shù)的結(jié)果做差除以QueryPerformanceFrequency()的運(yùn)作頻率即可求出在兩次“時(shí)間獲取”之間所經(jīng)過的時(shí)間。在其中放入想要測(cè)量時(shí)間的算法代碼,就可以得知算法的運(yùn)行時(shí)長(zhǎng)。
精度: 計(jì)算機(jī)獲取硬件支持,精度比較高,可以通過它判斷其他時(shí)間函數(shù)的精度范圍。
//QueryPerformanceCounter()是一個(gè)Windows API,所需頭文件為<windows.h> #include <windows.h> //引入頭文件 void time3() { LARGE_INTEGER t1, t2, tc; QueryPerformanceFrequency(&tc); QueryPerformanceCounter(&t1); fun(); //需計(jì)時(shí)的函數(shù) QueryPerformanceCounter(&t2); double time = (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart; cout << "elapsed time in QuadPart = " << time * 1000 << "ms" << endl; }
四、高精度計(jì)時(shí)chrono函數(shù)——納妙級(jí)
C++11 中的 chrono 庫提供了精度最高為納秒級(jí)的計(jì)時(shí)接口。由于是標(biāo)準(zhǔn)庫中提供的功能,所以可以很好地跨平臺(tái)使用。
下面來看一段使用 chrono 進(jìn)行高精度計(jì)時(shí)的示例代碼:
#include <iostream> #include <chrono> void time4() { // 計(jì)時(shí)開始時(shí)間點(diǎn) // chrone 中常用的時(shí)鐘類: // - std::chrono::high_resolution_clock // - std::chrono::system_clock // - std::chrono::steady_clock // 三種時(shí)鐘類有一些區(qū)別,其中 high_resolution_clock 精度最高 auto start = std::chrono::high_resolution_clock::now(); // 要計(jì)時(shí)的代碼段 fun(); // 計(jì)時(shí)結(jié)束時(shí)間點(diǎn) auto end = std::chrono::high_resolution_clock::now(); // 計(jì)算運(yùn)行時(shí)間, 時(shí)間單位: // - std::chrono::seconds // - std::chrono::milliseconds // - std::chrono::microseconds // - std::chrono::nanoseconds auto duration = std::chrono::duration_cast<std::chrono::microseconds> (end - start); // 輸出時(shí)間(給定時(shí)間單位) cout << "elapsed time in chrono = " << duration.count()/1000.0 << "ms" << endl; }
其中,microseconds 表示微妙。除此之外,還有五種時(shí)間單位:hours, minutes, seconds, milliseconds, nanoseconds.
五、幾種計(jì)時(shí)比較
#include <iostream> #include <chrono> #include <thread> #include <time.h> #include <windows.h> using namespace std; void fun() { // 睡眠100ms std::this_thread::sleep_for(std::chrono::milliseconds(100)); } void compareTime() { LARGE_INTEGER t1, t2, tc; QueryPerformanceFrequency(&tc); clock_t start, end; DWORD t11, t12; start = clock(); t11 = GetTickCount(); QueryPerformanceCounter(&t1); auto start1 = std::chrono::high_resolution_clock::now(); fun(); //需計(jì)時(shí)的函數(shù) end = clock(); t12 = GetTickCount(); QueryPerformanceCounter(&t2); auto end1 = std::chrono::high_resolution_clock::now(); double time = (double)(t2.QuadPart - t1.QuadPart) / (double)tc.QuadPart; auto duration = std::chrono::duration_cast<std::chrono::microseconds> (end1 - start1); cout << "elapsed time in clock = " << double(end - start) << "ms" << endl; cout << "elapsed time in GetTickCount = " << double(t12 - t11) << "ms" << endl; cout << "elapsed time in QuadPart = " << time * 1000 << "ms" << endl; cout << "elapsed time in chrono = " << duration.count() / 1000.0 << "ms" << endl; }
六、linux下的計(jì)時(shí)函數(shù)gettimeofday()-未測(cè)試
gettimeofday() linux環(huán)境下的計(jì)時(shí)函數(shù),int gettimeofday ( struct timeval * tv , struct timezone * tz ),gettimeofday()會(huì)把目前的時(shí)間由tv所指的結(jié)構(gòu)返回,當(dāng)?shù)貢r(shí)區(qū)的信息則放到tz所指的結(jié)構(gòu)中.
//timeval結(jié)構(gòu)定義為: struct timeval{ long tv_sec; /*秒*/ long tv_usec; /*微秒*/ }; //timezone 結(jié)構(gòu)定義為: struct timezone{ int tz_minuteswest; /*和Greenwich 時(shí)間差了多少分鐘*/ int tz_dsttime; /*日光節(jié)約時(shí)間的狀態(tài)*/ };
這個(gè)函數(shù)獲取從1970年1月1日到現(xiàn)在經(jīng)過的時(shí)間和時(shí)區(qū)(UTC時(shí)間),(按照linux的官方文檔,時(shí)區(qū)已經(jīng)不再使用,正常應(yīng)該傳NULL)。
調(diào)用代碼:
#include <sys/time.h> //引入頭文件 int main() { struct timeval t1,t2; double timeuse; gettimeofday(&t1,NULL); fun(); gettimeofday(&t2,NULL); timeuse = (t2.tv_sec - t1.tv_sec) + (double)(t2.tv_usec - t1.tv_usec)/1000000.0; cout<<"time = "<<timeuse<<endl; //輸出時(shí)間(單位:s) }
參考文獻(xiàn)
C++下四種常用的程序運(yùn)行時(shí)間的計(jì)時(shí)方法總結(jié)
C++中幾種測(cè)試程序運(yùn)行時(shí)間的方法
到此這篇關(guān)于C++高精度計(jì)時(shí)的幾種方法的文章就介紹到這了,更多相關(guān)C++高精度計(jì)時(shí)方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++實(shí)現(xiàn)簡(jiǎn)單貪吃蛇小游戲
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05QT基于TCP實(shí)現(xiàn)網(wǎng)絡(luò)聊天室程序
這篇文章主要為大家詳細(xì)介紹了QT基于TCP實(shí)現(xiàn)網(wǎng)絡(luò)聊天室程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08

C++實(shí)現(xiàn)反轉(zhuǎn)鏈表的兩種方法

C++ 非遞歸實(shí)現(xiàn)二叉樹的前中后序遍歷

C++中std::thread{}和std::thread()用法