0

I've searched SO and found relevant questions answered but I'm confused about the three different clock definitions. Considering I compile with Mingw on Windows;

  1. I wonder whether the code below is OK or not? (I do not really need nanoseconds or microseconds precision; just testing...)
  2. Which one should I use?
  • std::chrono::high_resolution_clock
  • std::chrono::system_clock
  • std::chrono::steady_clock
#include <iostream>
#include <chrono>
...
...
...
void printTimeElapsed(
    std::chrono::high_resolution_clock::time_point t0,
    std::chrono::high_resolution_clock::time_point t1)
{
    int64_t hh; // hour
    int64_t mm; // min
    int64_t ss; // sec
    int64_t ml; // millisec
    int64_t mc; // microsec
    int64_t ns; // nanosec

    ns = std::chrono::duration_cast<std::chrono::nanoseconds>(t1 - t0).count();
    std::cout << ns << std::endl;
    mc = ns / 1000;
    ns %= 1000;
    ml = mc / 1000;
    mc %= 1000;
    ss = ml / 1000;
    ml %= 1000;
    mm = ss / 60;
    ss %= 60;
    hh = mm / 60;
    mm %= 60;

    std::cout
        << std::setfill('0') << std::setw(3) << hh << ":"
        << std::setfill('0') << std::setw(2) << mm << ":"
        << std::setfill('0') << std::setw(2) << ss << "."
        << std::setfill('0') << std::setw(3) << ml << "."
        << std::setfill('0') << std::setw(3) << mc << "."
        << std::setfill('0') << std::setw(3) << ns << std::endl;
    return;
}
...
...
...
int main(
    int argc,
    char *argv[])
{
    std::chrono::high_resolution_clock::time_point t0;
    std::chrono::high_resolution_clock::time_point t1;
    ...
    ...
    t0 = std::chrono::high_resolution_clock::now();
    /* call the function to be measured */
    t1 = std::chrono::high_resolution_clock::now();
    printTimeElapsed(t0, t1);
    ...
    ...
    return (0);       
}
ssd
  • 2,340
  • 5
  • 19
  • 37
  • 2
    std::chrono::steady_clock – drescherjm Oct 20 '21 at 04:05
  • 3
    Definitely `steady_clock`. `system_clock` can go backwards and `high_res_clock` is unreliable because it can be anything e.g. an alias for `sysyem_clock`. – bolov Oct 20 '21 at 04:07
  • 3
    Related: [https://stackoverflow.com/questions/31552193/difference-between-steady-clock-vs-system-clock](https://stackoverflow.com/questions/31552193/difference-between-steady-clock-vs-system-clock) – drescherjm Oct 20 '21 at 04:07
  • Like you, I used `std::chrono::high_resolution_clock` but it isn't consistently between SO, so the recommendation is using `std::chrono::steady_clock` or `std::chrono::system_clock`. Check the note in https://en.cppreference.com/w/cpp/chrono/high_resolution_clock – Jorge Omar Medra Oct 20 '21 at 04:41
  • `std::chrono::steady_clock` – justANewb stands with Ukraine Oct 20 '21 at 06:00

1 Answers1

-1

system_clock is not steady: is subject to time adjustments. high_resolution_clock is not guaranteed to be steady.

It means for users to use steady_clock for measurement, and because of this, implementers need to make the steady_clock of a high resolution.

Windows implementation of steady_clock should be based on QueryPerformanceCounter / QueryPerformanceFrequency, which is highest resolution of available API. This is true for MSVC, need to check for MinGW.

Alex Guteniev
  • 12,039
  • 2
  • 34
  • 79