想給自己初步完成的相空間搜索算法計算一下運行時間,于是嘗試了如下使用 time_t 類型的方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#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; } |
這種實現方式可以正確計算出算法的核心部分耗費了234秒的 walltime。在此之前嘗試的使用 clock_t 類型的實現方式是
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#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秒,明顯不對。造成這種結果的原因暫時還不清楚,或許是因為算法執行過程中在頻繁調用其他外部程序來獲得一些計算結果。
以上就是小編為大家帶來的計時器的time_t和clock_t 的兩種實現方法(推薦)全部內容了,希望大家多多支持服務器之家~