C++ - everything to know about Time and Timers

No comments

C++ - everything to know about Time and Timers

For years, since 1983 when C++ was first released, developers were stuck with C time library, packed in ctime header file. It contains a couple of functions and precision up to a second sometimes just isn't enough. For everything else clock() should be used or OS (Operating System) provided function, which really breaks code portability. In C++11 things got much better, with Chrono module.

For formating ctime function from good ol' C will still be required but for timers there are now classes that go down to a nanosecond precision without any work necessary.


There are three timers:

  • system_clock - goes as operating system goes
  • steady_clock - ensures that time measured is the same as physical time passed
  • high_resolution_clock - the name already says everything - it's intention is maximum precision.

Use of Timers

Everything you need is packed in namespace std::chrono. In example bellow high_resolution_clock is used:

#include <iostream>
#include <chrono>

using namespace std;
using namespace chrono;

int fib(int x) 
    if (x <= 1)
        return x;

    return fib(x - 1) + fib(x - 2);

template<typename Func>
void measure_time(Func &&f)
    auto start = high_resolution_clock::now();


    auto end = high_resolution_clock::now();
    auto time_span = duration_cast<duration<double>>(end - start);

    cout << "It took me " << time_span.count() << " seconds." << endl;

int main()

    return 0;

The above example prints 0.0770526 seconds on my PC. Everything can be of course beautifully wrapped in a template function that take lambda expression (code to measure) as a parameter for maximum re-usability.

time_point and duration

All time operations return time_point or duration. Duration is highly dependent on ration class, to specify it's length. This example:

duration<int, ratio<3600, 1>>(1)

is equivalent to 1 hour. If we would replace 3600 with 60, 360 would be 6 minutes etc. Statement above would represent 1 minute. General time spans (hour, minute, second, millisecond ...) are already typedef-ed for us in standard library. In addition, every operator is overloaded, so adding durations and calculating differences is just a matter of one plus or minus sign.

system_clock::time_point tp;
tp  = tp + hours(30) + minutes(21) + seconds(14);

std::time_t tt = system_clock::to_time_t(tp);
std::cout << "time_point tp is: " << ctime(&tt);

The above codes print "Fri Jan 02 07:21:14 1970". Keep in mind, that every time point starts at start of Unix time, 1st of January 1970.


For formating, fall-back to default ctime function is unfortunately still needed, but everything is else is really a major step back in this area. It is still far away from libraries like boost, Qt or implementations in C# or Java, but eventually, we'll get there.

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.