| 10452 | } |
| 10453 | |
| 10454 | auto estimateClockResolution() -> uint64_t { |
| 10455 | uint64_t sum = 0; |
| 10456 | static const uint64_t iterations = 1000000; |
| 10457 | |
| 10458 | auto startTime = getCurrentNanosecondsSinceEpoch(); |
| 10459 | |
| 10460 | for (std::size_t i = 0; i < iterations; ++i) { |
| 10461 | |
| 10462 | uint64_t ticks; |
| 10463 | uint64_t baseTicks = getCurrentNanosecondsSinceEpoch(); |
| 10464 | do { |
| 10465 | ticks = getCurrentNanosecondsSinceEpoch(); |
| 10466 | } while (ticks == baseTicks); |
| 10467 | |
| 10468 | auto delta = ticks - baseTicks; |
| 10469 | sum += delta; |
| 10470 | |
| 10471 | // If we have been calibrating for over 3 seconds -- the clock |
| 10472 | // is terrible and we should move on. |
| 10473 | // TBD: How to signal that the measured resolution is probably wrong? |
| 10474 | if (ticks > startTime + 3 * nanosecondsInSecond) { |
| 10475 | return sum / i; |
| 10476 | } |
| 10477 | } |
| 10478 | |
| 10479 | // We're just taking the mean, here. To do better we could take the std. dev and exclude outliers |
| 10480 | // - and potentially do more iterations if there's a high variance. |
| 10481 | return sum / iterations; |
| 10482 | } |
| 10483 | auto getEstimatedClockResolution() -> uint64_t { |
| 10484 | static auto s_resolution = estimateClockResolution(); |
| 10485 | return s_resolution; |
no test coverage detected