Boost logo

Boost :

From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2022-06-21 11:01:04


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
>
> https://godbolt.org/z/bcGPcebP1

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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk