0

In my software I collect some data and i need to attach to the data a time (in us). I tried to use

std::chrono::high_resolution_clock::now() 

but it seems to have ~500uS of resolution

#include <chrono>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
    using namespace std::chrono;
    int step = 0;
    while (step < 100){
        static int cnt = 1;
        static high_resolution_clock::time_point t1 = high_resolution_clock::now();
        high_resolution_clock::time_point t2 = high_resolution_clock::now();
        if (t1 != t2) {
            if (step++) std::cout << cnt << std::endl;
            cnt = 1;
            long long microseconds = duration_cast<std::chrono::microseconds>(t2 - t1).count();
            std::cout << microseconds << "  x";
            t1 = t2;
        }
        else
            cnt++;

    }
    std::cin.ignore();
    return 0;
}

Results shows that t2 changes only if there is at least 500us between the previous call:

502     x1
1003    x1
1007    x1
1001    x1
999     x1
517     x1031
485     x1286
514     x909
487     x1008
499     x931
501     x1515
502     x1382
500     x1138
501     x437
501     x1
512     x95
491     x1
499     x139
502     x591
506     x694
496     x856
501     x1474
500     x1431
501     x1649
501     x1435
501     x1653
501     x1433
501     x769
501     x1442
501     x1067
501     x1
5520    x1
996     x1
1003    x1
501     x1
1001    x1
502     x1
1001    x1

Is there a better way to increase the precision?

Thanks

brazoayeye
  • 329
  • 1
  • 2
  • 14
  • What compiler/os are you using? Possibly related:https://stackoverflow.com/questions/16299029/resolution-of-stdchronohigh-resolution-clock-doesnt-correspond-to-measureme – freakish Feb 24 '18 at 11:17
  • 1
    On Windows (until two years ago Windows 7, since then Windows 10), I was never able to measure less than 500 us. Actually, I expected 1 ms. This is with increased timer resolution (e.g. by activating multimedia timers). Without, it was round about 15 ms (except another multimedia appl. was running "by accident" e.g. a browser with streaming). I'm sure there are ways to measure time with even more resolution but the implementation had to be platform specific. (As 1 ms was sufficient for me, and I prefer portable code I didn't investigate in this.) – Scheff's Cat Feb 24 '18 at 11:36
  • 2
    You probably have an old version of VS, like VS2012 or VS2013. They took a dumb shortcut at first and high_resolution_clock isn't actually high resolution. They used the regular clock, the one you can mess with like that. Either update or use QueryPerformanceCounter(). – Hans Passant Feb 24 '18 at 11:37
  • I recommend you use `std::chrono::steady_clock` as it is guaranteed to be monatonic. The `high_resolution_clock` is often just a type alias of the `system_clock` which often gets updated in the background (minor automatic corrections from the internet for example) to keep the "wall time" accurate. – Galik Feb 24 '18 at 12:43
  • Yes, you are right. I'm using VS2013 and I can't upddate since I'm using cern root. I will probably interpolate data to solve my problem. – brazoayeye Feb 24 '18 at 15:21

0 Answers0