計時器的time_t和clock_t 的兩種實現(xiàn)方法(推薦)
想給自己初步完成的相空間搜索算法計算一下運行時間,于是嘗試了如下使用 time_t 類型的方式
#include <stdlib.h> #include <iostream> #include <time.h> #include "StateFunctions.h" using namespace std; int main(int argc, char** argv) { time_t start, finish; time(&start); StateFunctions testobj(22, 22); testobj.TEST(); testobj.TEST(); testobj.FillRandomDets(200); testobj.evolute(1000, 0.9); cout << "--------------------------------------------" << endl; time(&finish); double duration = difftime(finish, start); cout << "--> time: " << duration << " s" << endl; cout << "--------------------------------------------" << endl; return 0; }
這種實現(xiàn)方式可以正確計算出算法的核心部分耗費了234秒的 walltime。在此之前嘗試的使用 clock_t 類型的實現(xiàn)方式是
#include <iostream> #include <time.h> #include "StateFunctions.h" using namespace std; int main(int argc, char** argv) { clock_t start, finish; start = clock(); StateFunctions testobj(22, 22); testobj.TEST(); testobj.TEST(); testobj.FillRandomDets(200); testobj.evolute(1000, 0.9); cout << "--------------------------------------------" << endl; finish = clock(); double duration = (double)(finish - start) / CLOCKS_PER_SEC; cout << "--> time: " << duration << " s" << endl; cout << "--------------------------------------------" << endl; return 0; }
這段代碼得到的運行時間只有11秒,明顯不對。造成這種結(jié)果的原因暫時還不清楚,或許是因為算法執(zhí)行過程中在頻繁調(diào)用其他外部程序來獲得一些計算結(jié)果。
以上就是小編為大家?guī)淼挠嫊r器的time_t和clock_t 的兩種實現(xiàn)方法(推薦)全部內(nèi)容了,希望大家多多支持腳本之家~
相關(guān)文章
C++實現(xiàn)LeetCode(48.旋轉(zhuǎn)圖像)
這篇文章主要介紹了C++實現(xiàn)LeetCode(48.旋轉(zhuǎn)圖像),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C語言 數(shù)據(jù)結(jié)構(gòu)中求解迷宮問題實現(xiàn)方法
這篇文章主要介紹了C語言 數(shù)據(jù)結(jié)構(gòu)中求解迷宮問題實現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-03-03Qt5.9.5 隨機(jī)轉(zhuǎn)盤小項目的實現(xiàn)示例
本文主要介紹了Qt5.9.5隨機(jī)轉(zhuǎn)盤小項目的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06