MCPcopy Create free account
hub / github.com/ChaiScript/ChaiScript / estimateClockResolution

Function estimateClockResolution

unittests/catch.hpp:10532–10560  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

10530 }
10531
10532 auto estimateClockResolution() -> uint64_t {
10533 uint64_t sum = 0;
10534 static const uint64_t iterations = 1000000;
10535
10536 auto startTime = getCurrentNanosecondsSinceEpoch();
10537
10538 for( std::size_t i = 0; i < iterations; ++i ) {
10539
10540 uint64_t ticks;
10541 uint64_t baseTicks = getCurrentNanosecondsSinceEpoch();
10542 do {
10543 ticks = getCurrentNanosecondsSinceEpoch();
10544 } while( ticks == baseTicks );
10545
10546 auto delta = ticks - baseTicks;
10547 sum += delta;
10548
10549 // If we have been calibrating for over 3 seconds -- the clock
10550 // is terrible and we should move on.
10551 // TBD: How to signal that the measured resolution is probably wrong?
10552 if (ticks > startTime + 3 * nanosecondsInSecond) {
10553 return sum / i;
10554 }
10555 }
10556
10557 // We're just taking the mean, here. To do better we could take the std. dev and exclude outliers
10558 // - and potentially do more iterations if there's a high variance.
10559 return sum/iterations;
10560 }
10561 auto getEstimatedClockResolution() -> uint64_t {
10562 static auto s_resolution = estimateClockResolution();
10563 return s_resolution;

Callers 1

Calls 1

Tested by

no test coverage detected