我正在爲我必須爲一個類編寫的數據結構編寫一個深入的測試程序。我試圖花費多長時間來執行函數,並將它們存儲在數組中以供以後打印。要仔細檢查它是否正在工作,我決定立即打印它,並且我發現它不起作用。計時函數:雙重返回0 MS
這裏是我得到時間的代碼,並將它們存儲在一個結構體中的數組中。
void test1(ArrayLinkedBag<ItemType> &bag,TestAnalytics &analytics){
clock_t totalStart;
clock_t incrementalStart;
clock_t stop; //Both timers stop at the same time;
// Start TEST 1
totalStart = clock();
bag.debugPrint();
cout << "Bag Should Be Empty, Checking..." << endl;
incrementalStart = clock();
checkEmpty<ItemType>(bag);
stop = clock();
analytics.test1Times[0] = analytics.addTimes(incrementalStart,stop);
analytics.test1Times[1] = analytics.addTimes(totalStart,stop);
cout << analytics.test1Times[0] << setprecision(5) << "ms" << endl;
std::cout << "Time: "<< setprecision(5) << (stop - totalStart)/(double)(CLOCKS_PER_SEC/1000) << " ms" << std::endl;
cout << "===========================================" << endl; //So I can find the line easier
}
這裏就是我做的,我在數組中我把計算的代碼,該功能位於TestAnalytics結構
double addTimes(double start, double stop){
return (stop - start)/ (double)(CLOCKS_PER_SEC/1000);
}
下面是我得到的輸出的一個片段:
Current Head: -1
Current Size: 0
Cell: 1, Index: 0, Item: 6317568, Next Index: -2
Cell: 2, Index: 1, Item: 4098, Next Index: -2
Cell: 3, Index: 2, Item: 6317544, Next Index: -2
Cell: 4, Index: 3, Item: -683175280, Next Index: -2
Cell: 5, Index: 4, Item: 4201274, Next Index: -2
Cell: 6, Index: 5, Item: 6317536, Next Index: -2
Bag Should Be Empty, Checking...
The Bag Is Empty
0ms
Time: 0 ms
===========================================
我想根據此網站上的其他帖子計算時間。 我在UNIX系統上使用clang編譯器。這個數字是否可能太小而不能顯示在0以上?
'CLOCKS_PER_SEC/1000'可能是整數除法;不知道這是否與你的問題有關 –
@ M.M我把這個翻譯成了兩個。我剛剛注意到,我的stop和totalStart都打印了0. –
更改爲'1000.0',在你已經完成整數除法之後沒有任何一點投射到一個加倍 –