我正在为我必须为一个类编写的数据结构编写一个深入的测试程序。我试图花费多长时间来执行函数,并将它们存储在数组中以供以后打印。要仔细检查它是否正在工作,我决定立即打印它,并且我发现它不起作用。计时函数:双重返回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',在你已经完成整数除法之后没有任何一点投射到一个加倍 –