2017-10-06 101 views
0

我正在为我必须为一个类编写的数据结构编写一个深入的测试程序。我试图花费多长时间来执行函数,并将它们存储在数组中以供以后打印。要仔细检查它是否正在工作,我决定立即打印它,并且我发现它不起作用。计时函数:双重返回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以上?

+0

'CLOCKS_PER_SEC/1000'可能是整数除法;不知道这是否与你的问题有关 –

+0

@ M.M我把这个翻译成了两个。我刚刚注意到,我的stop和totalStart都打印了0. –

+0

更改为'1000.0',在你已经完成整数除法之后没有任何一点投射到一个加倍 –

回答

1

除非你坚持使用旧的(预C++ 11)编译器/库,我会使用的功能从<chrono>头:

template <class ItemType> 
void test1(ArrayLinkedBag<ItemType> &bag){ 
    using namespace std::chrono; 

    auto start = high_resolution_clock::now(); 
    bag.debugPrint(); 
    auto first = high_resolution_clock::now(); 
    checkEmpty(bag); 
    auto stop = high_resolution_clock::now(); 

    std::cout << " first time: " << duration_cast<microseconds>(first - start).count() << " us\n"; 
    std::cout << "second time: " << duration_cast<microseconds>(stop - start).count() << " us\n"; 
} 

有些部分有点冗长(把它很好),但它仍然工作得很好。 duration_cast支持差分类型降至(至少)nanoseconds,这通常足以支持相对较小/较快的代码段定时(尽管不能保证它使用纳秒精度的定时器)。

+0

谢谢,我会工作明天当我回来时试试这个,但我会告诉你。 –

1

除了Jerry's good answer(我已经投了票),我想添加一些可能有用的信息。

对于时间我推荐steady_clock超过high_resolution_clock,因为steady_clock保证不会在您的计时期间调整(特别是向后)。现在在Visual Studio和叮当中,这不可能发生,因为high_resolution_clocksteady_clock是完全相同的类型。但是,如果您使用的是gcc,则high_resolution_clocksystem_clock的类型相同,可随时进行调整(例如通过NTP更正)。

但是,如果您使用steady_clock,那么在每个平台上都有一个类似秒表的计时器:不适合告诉您一天的时间,但不会在不合时宜的情况下被纠正。

此外,如果您使用my free, open-source, header-only <chrono> extension library,它可以以更友好的方式输出持续时间,而不必使用duration_cast.count()。它会随着价值打印持续时间单位。

最后,如果您连续多次调用steady_clock::now()(并在两者之间没有任何内容),并打印出该差异,则可以了解您的实现能够如何精确地计时。它的时间可以短到飞秒吗?可能不会。它是否像毫秒一样粗?我们希望不会。

把所有这些组合起来,下面的程序编译如下:

clang++ test.cpp -std=c++14 -O3 -I../date/include 

程序:

#include "date/date.h" 
#include <iostream> 

int 
main() 
{ 
    using namespace std::chrono; 
    using date::operator<<; 
    for (int i = 0; i < 100; ++i) 
    { 
     auto t0 = steady_clock::now(); 
     auto t1 = steady_clock::now(); 
     auto t2 = steady_clock::now(); 
     auto t3 = steady_clock::now(); 
     auto t4 = steady_clock::now(); 
     auto t5 = steady_clock::now(); 
     auto t6 = steady_clock::now(); 
     std::cout << t1-t0 << '\n'; 
     std::cout << t2-t1 << '\n'; 
     std::cout << t3-t2 << '\n'; 
     std::cout << t4-t3 << '\n'; 
     std::cout << t5-t4 << '\n'; 
     std::cout << t6-t5 << '\n'; 
    } 
} 

和输出为我在MacOS:

150ns 
80ns 
69ns 
53ns 
63ns 
64ns 
88ns 
54ns 
66ns 
66ns 
59ns 
56ns 
59ns 
69ns 
76ns 
74ns 
73ns 
73ns 
64ns 
60ns 
58ns 
...