| 17357 | |
| 17358 | namespace { |
| 17359 | auto 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 | } |
| 17390 | auto getEstimatedClockResolution() -> uint64_t |
| 17391 | { |
no test coverage detected