55 static inline double get_clock_precision()
57 static double clock_precision{-1.0};
58 static std::mutex mtx;
59 std::lock_guard<std::mutex> lock(mtx);
61 if (clock_precision == -1.0)
63 using namespace std::string_literals;
66 LARGE_INTEGER frequency;
67 if (QueryPerformanceFrequency(&frequency))
69 clockPrecision = 1.0 / frequency.QuadPart;
71#elif defined(__linux__)
73 if (clock_getres(CLOCK_MONOTONIC, &ts) == 0)
75 clock_precision = ts.tv_sec + ts.tv_nsec * 1e-9;
77#elif defined(__APPLE__)
78 mach_timebase_info_data_t info;
79 if (mach_timebase_info(&info) == 0)
81 clockPrecision = (double)info.numer / info.denom * 1e-9;
85 if (clock_precision == -1.0)
87 CBEAM_LOG(
"Unable to determine resolution of high resolution clock, using fallback algorithm"s);
91 auto startTime = std::chrono::high_resolution_clock::now();
95 result = std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - startTime).count();
96 }
while (result == 0.0);
98 clock_precision = std::min(clock_precision, result);
103 return clock_precision;