
On 6/21/22 12:35, Richard Hodges via Boost wrote:
#include <chrono> #include <concepts> #include <iostream> #include <iomanip> #include <ratio> #include <unistd.h>
int main() { using clock_type = std::chrono::high_resolution_clock; using namespace std::literals;
auto t = clock_type::now(); auto const t0 = t; auto const t1 = t0 + 1s; while(t < t1) { ::usleep(1); t = clock_type::now(); }
std::cout << std::chrono::duration_cast<std::chrono::duration<double, std::milli>>(t - t0).count() << "ms\n"; }
Example output:
Program returned: 0 1000.47ms
Timer precision may depend on the sleep duration. Change to sleep for 10 seconds and you get drift in the order of milliseconds: 10006.5ms https://godbolt.org/z/Gjxccz5bK