| 14645 | |
| 14646 | namespace { |
| 14647 | auto estimateClockResolution() -> uint64_t { |
| 14648 | uint64_t sum = 0; |
| 14649 | static const uint64_t iterations = 1000000; |
| 14650 | |
| 14651 | auto startTime = getCurrentNanosecondsSinceEpoch(); |
| 14652 | |
| 14653 | for( std::size_t i = 0; i < iterations; ++i ) { |
| 14654 | |
| 14655 | uint64_t ticks; |
| 14656 | uint64_t baseTicks = getCurrentNanosecondsSinceEpoch(); |
| 14657 | do { |
| 14658 | ticks = getCurrentNanosecondsSinceEpoch(); |
| 14659 | } while( ticks == baseTicks ); |
| 14660 | |
| 14661 | auto delta = ticks - baseTicks; |
| 14662 | sum += delta; |
| 14663 | |
| 14664 | // If we have been calibrating for over 3 seconds -- the clock |
| 14665 | // is terrible and we should move on. |
| 14666 | // TBD: How to signal that the measured resolution is probably wrong? |
| 14667 | if (ticks > startTime + 3 * nanosecondsInSecond) { |
| 14668 | return sum / ( i + 1u ); |
| 14669 | } |
| 14670 | } |
| 14671 | |
| 14672 | // We're just taking the mean, here. To do better we could take the std. dev and exclude outliers |
| 14673 | // - and potentially do more iterations if there's a high variance. |
| 14674 | return sum/iterations; |
| 14675 | } |
| 14676 | } |
| 14677 | auto getEstimatedClockResolution() -> uint64_t { |
| 14678 | static auto s_resolution = estimateClockResolution(); |
no test coverage detected