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

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