Measure elapsed time with chrono lib in C++

·

2 min read

Chrono is a flexible collection of types that track time with varying degrees of precision. Since C++ 11, one of the best ways to deal with time is using it.
We will use chrono to measure the average execution time of some functions.

To use chrono we first need to include it:

#include <chrono>

We now have access to some cool types like calendar specific things (day, month, year, weekday and so on), clocks (system_clock, high_resolution_clock and so on) and some other stuff.
Here we will focus on steady_clock, this is a monotonic clock that will never be adjusted(at least that's what it says in the documentation).
We will need to get the time before the execution of the code that we are measuring time and the time after the execution, something like this:

auto start = std::chrono::steady_clock::now();
someMethod();
auto end = std::chrono::steady_clock::now();

Now we can subtract the start from the end to get spended time and do a cast using duration_cast (it will make a cast to a specific duration type you want, like nanoseconds, microseconds, milliseconds or seconds).

chrono::duration_cast<chrono::milliseconds>(end - start).count()

We call count() at the end so we can get the result as a int64_t.
Here is a complete program example that uses it:

#include <iostream>
#include <chrono>
#include <thread>
#include <atomic>
#include <mutex>

void sharedDataExample()
{
    std::atomic<int> count { 0 };
    const int INTERATIONS = 1E6;

    std::thread t1([&count](){
        for (int i = 0; i < INTERATIONS; i++)
        {
            ++count;
        }
    });
    std::thread t2([&count](){
        for (int i = 0; i < INTERATIONS; i++)
        {
            ++count;
        }
    });

    t1.join();
    t2.join();

    std::cout << "Count: " << count << '\n';
}

void mutexExample()
{
    std::atomic<int> count { 0 };
    const int INTERATIONS = 1E6;
    std::mutex mutexThing;

    auto func = [&](){
        for (int i = 0; i < INTERATIONS; i++)
        {
            mutexThing.lock();
            ++count;
            mutexThing.unlock();
        }
    };

    std::thread t1(func);
    std::thread t2(func);

    t1.join();
    t2.join();

    std::cout << "Count: " << count << '\n';
}

int main()
{
    auto start = std::chrono::steady_clock::now();
    sharedDataExample();
    auto end = std::chrono::steady_clock::now();

    std::cout << "Elapsed time in milliseconds: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << '\n';

    start = std::chrono::steady_clock::now();
    mutexExample();
    end = std::chrono::steady_clock::now();

    std::cout << "Elapsed time in milliseconds: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << '\n';

    return 0;
}

Here are the results:

You can find more about chrono at:

cppreference

There are other ways to measure time of course, but this is one of the easiest. I hope you enjoy this little piece of utility.

Did you find this article valuable?

Support Doors of stone by becoming a sponsor. Any amount is appreciated!