MCPcopy Create free account
hub / github.com/WarmUpTill/SceneSwitcher / estimateClockResolution

Function estimateClockResolution

tests/catch.hpp:17359–17388  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17357
17358namespace {
17359auto estimateClockResolution() -> uint64_t
17360{
17361 uint64_t sum = 0;
17362 static const uint64_t iterations = 1000000;
17363
17364 auto startTime = getCurrentNanosecondsSinceEpoch();
17365
17366 for (std::size_t i = 0; i < iterations; ++i) {
17367
17368 uint64_t ticks;
17369 uint64_t baseTicks = getCurrentNanosecondsSinceEpoch();
17370 do {
17371 ticks = getCurrentNanosecondsSinceEpoch();
17372 } while (ticks == baseTicks);
17373
17374 auto delta = ticks - baseTicks;
17375 sum += delta;
17376
17377 // If we have been calibrating for over 3 seconds -- the clock
17378 // is terrible and we should move on.
17379 // TBD: How to signal that the measured resolution is probably wrong?
17380 if (ticks > startTime + 3 * nanosecondsInSecond) {
17381 return sum / (i + 1u);
17382 }
17383 }
17384
17385 // We're just taking the mean, here. To do better we could take the std. dev and exclude outliers
17386 // - and potentially do more iterations if there's a high variance.
17387 return sum / iterations;
17388}
17389}
17390auto getEstimatedClockResolution() -> uint64_t
17391{

Callers 1

Calls 1

Tested by

no test coverage detected