Measure elapsed time of a C++ program using Chrono

In this tutorial, we will learn how to measure elapsed time in C++ using the Chrono library. Hope you will find this tutorial helpful for you as well as you can use this method to calculate elapsed time in your projects and know in-depth the use of elapsed time in C++.

So, I’m going to make elapsed time tutorial interesting for you to learn and gain in-depth knowledge about the same. Hope you like this tutorial.

Elapsed time in C++

To calculate elapsed time in C++ why we used the Chrono library. Since Chrono library in C++ is related to date and time. Every system has different timers and clocks and to improve time with precision we can make use of this library.

Here we will measure elapsed time in the C++ program in seconds, milliseconds, microseconds, and nanoseconds.

This include <chrono.h> header file, which provides access to system_clock() to calculate the current time. As system_clock() is designed to represent real-time which shows the running time of the system used by various processes.

Sample Code:

#include <iostream>
#include <chrono>   // chrono library
#include <unistd.h>
using namespace std;

int main()
    auto start = chrono::steady_clock::now();
 //here we were doing some basic stuff.
    auto end = chrono::steady_clock::now();
    cout << "Elapsed time in seconds: "
        << chrono::duration_cast<chrono::seconds>(end - start).count()
        << " sec"<< endl;
    cout << "Elapsed time in milliseconds: "
        << chrono::duration_cast<chrono::milliseconds>(end - start).count()
        << " ms" << endl;
    return 0;


It is dependent on machines and may vary from one system to another.

Elapsed time in seconds: 3 sec
Elapsed time in milliseconds: 3000 ms

Hope this tutorial is helpful for you. Happy Learning !!

Also Read:

Leave a Reply

Your email address will not be published. Required fields are marked *