C++實(shí)現(xiàn)統(tǒng)計(jì)代碼運(yùn)行時(shí)間計(jì)時(shí)器的簡單實(shí)例
C++實(shí)現(xiàn)統(tǒng)計(jì)代碼運(yùn)行時(shí)間計(jì)時(shí)器的簡單實(shí)例
一、前言
這里記下從網(wǎng)上找到的一些自己比較常用的C++計(jì)時(shí)代碼
二、Linux下精確至毫秒
#include <sys/time.h> #include <iostream> #include <time.h> double get_wall_time() { struct timeval time ; if (gettimeofday(&time,NULL)){ return 0; } return (double)time.tv_sec + (double)time.tv_usec * .000001; } int main() { unsigned int t = 0; double start_time = get_wall_time() while(t++<10e+6); double end_time = get_wall_time() std::cout<<"循環(huán)耗時(shí)為:"<<end_time-start_time<<"ms"; return 0; }
三、Windows下精確至毫秒
#include <windows.h> #include <iostream> int main() { DWORD start, stop; unsigned int t = 0; start = GetTickCount(); while (t++ < 10e+6); stop = GetTickCount(); printf("time: %lld ms\n", stop - start); return 0; }
試驗(yàn)中,發(fā)現(xiàn)貌似getTickCount函數(shù)會(huì)有10幾毫秒的誤差,囧。。。
四、Windows下精確至微秒
//MyTimer.h// #ifndef __MyTimer_H__ #define __MyTimer_H__ #include <windows.h> class MyTimer { private: int _freq; LARGE_INTEGER _begin; LARGE_INTEGER _end; public: long costTime; // 花費(fèi)的時(shí)間(精確到微秒) public: MyTimer() { LARGE_INTEGER tmp; QueryPerformanceFrequency(&tmp);//QueryPerformanceFrequency()作用:返回硬件支持的高精度計(jì)數(shù)器的頻率。 _freq = tmp.QuadPart; costTime = 0; } void Start() // 開始計(jì)時(shí) { QueryPerformanceCounter(&_begin);//獲得初始值 } void End() // 結(jié)束計(jì)時(shí) { QueryPerformanceCounter(&_end);//獲得終止值 costTime = (long)((_end.QuadPart - _begin.QuadPart) * 1000000 / _freq); } void Reset() // 計(jì)時(shí)清0 { costTime = 0; } }; #endif //main.cpp #include "MyTimer.h" #include <iostream> int main() { MyTimer timer; unsigned int t = 0; timer.Start(); while (t++ < 10e+5); timer.End(); std::cout << "耗時(shí)為:" << timer.costTime << "us"; return 0 ; }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
上一篇:C++ 隨機(jī)數(shù)與隨機(jī)種子數(shù)的實(shí)例
欄 目:C語言
下一篇:C++中靜態(tài)成員函數(shù)訪問非靜態(tài)成員的實(shí)例
本文標(biāo)題:C++實(shí)現(xiàn)統(tǒng)計(jì)代碼運(yùn)行時(shí)間計(jì)時(shí)器的簡單實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/1368.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery