| 14934 | |
| 14935 | namespace { |
| 14936 | auto estimateClockResolution() -> uint64_t { |
| 14937 | uint64_t sum = 0; |
| 14938 | static const uint64_t iterations = 1000000; |
| 14939 | |
| 14940 | auto startTime = getCurrentNanosecondsSinceEpoch(); |
| 14941 | |
| 14942 | for( std::size_t i = 0; i < iterations; ++i ) { |
| 14943 | |
| 14944 | uint64_t ticks; |
| 14945 | uint64_t baseTicks = getCurrentNanosecondsSinceEpoch(); |
| 14946 | do { |
| 14947 | ticks = getCurrentNanosecondsSinceEpoch(); |
| 14948 | } while( ticks == baseTicks ); |
| 14949 | |
| 14950 | auto delta = ticks - baseTicks; |
| 14951 | sum += delta; |
| 14952 | |
| 14953 | // If we have been calibrating for over 3 seconds -- the clock |
| 14954 | // is terrible and we should move on. |
| 14955 | // TBD: How to signal that the measured resolution is probably wrong? |
| 14956 | if (ticks > startTime + 3 * nanosecondsInSecond) { |
| 14957 | return sum / ( i + 1u ); |
| 14958 | } |
| 14959 | } |
| 14960 | |
| 14961 | // We're just taking the mean, here. To do better we could take the std. dev and exclude outliers |
| 14962 | // - and potentially do more iterations if there's a high variance. |
| 14963 | return sum/iterations; |
| 14964 | } |
| 14965 | } |
| 14966 | auto getEstimatedClockResolution() -> uint64_t { |
| 14967 | static auto s_resolution = estimateClockResolution(); |
no test coverage detected