3

I'm trying to get my code to measure time durations shorter than 1ms but just can't.

I've searched around but have not managed to understand how to do it. I've added various bits of code I found that I think are relevant.

template <class Clock>
void
display_precision()
{
    typedef std::chrono::duration<double, std::nano> NS;
    NS ns = typename Clock::duration(1);
    std::cout << ns.count() << " ns\n";
}


int main()
{


    display_precision<std::chrono::high_resolution_clock>();
    display_precision<std::chrono::system_clock>();
    display_precision<std::chrono::steady_clock>();
    std::cout << std::chrono::high_resolution_clock::period::num 
        << "/" << std::chrono::high_resolution_clock::period::den;


    std::chrono::high_resolution_clock::time_point nowTime;
    std::chrono::high_resolution_clock::time_point startTime;
    startTime = std::chrono::high_resolution_clock::now();

    int count = 0;
    do
    {
        nowTime = std::chrono::high_resolution_clock::now();
        std::chrono::high_resolution_clock::duration diff = nowTime - startTime;
        __int64 difference = std::chrono::duration_cast<std::chrono::microseconds>(diff).count();

        printf("\n%i", difference);
        count++;

        if (std::chrono::duration_cast<std::chrono::seconds>(diff).count() > 2) { break; }

    } while (1);

}

The out put I get looks like this:

100 ns
100 ns
100 ns
1 / 10000000

and:

2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2996299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2997299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2998299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
2999299
3000300

As you can see, the difference changes once a msec and remains constant in between although it loops 15 times or so in the meantime (and even though I reportedly have a 100ns precision).

Also, this doesn't seem to be a printf bottleneck because I tried loading a vector with the data and I get the same results.

[Windows 7 Professional, VS Community 2013]

Laurel
  • 5,965
  • 14
  • 31
  • 57
pope
  • 23
  • 4
  • What OS are you on? Kernel version? Compiler version? C/C++ library version? And so on... About chrono, here is a good link: http://stackoverflow.com/questions/8386128/how-to-get-the-precision-of-high-resolution-clock – Erik Alapää Oct 22 '15 at 14:35
  • This looks like Visual Studio, not sure which version. – Howard Hinnant Oct 22 '15 at 17:00
  • Sorry. Edited my post. Windows 7 Professional, VS Community 2013 – pope Oct 22 '15 at 20:53
  • To Erik's comment: Yes I've seen this thread and some of the code I used to test on my machine is from there. – pope Oct 27 '15 at 08:34

1 Answers1

1

This is a problem with VS2013, fixed in VS2015. From: Visual Studio 2015 RTM details

"<chrono> The chrono types high_resolution_clock and steady_clock have been fixed. C++11"

janm
  • 17,976
  • 1
  • 43
  • 61