Open In App

How to Measure Elapsed Time in C++?

Improve
Improve
Like Article
Like
Save
Share
Report

Measuring elapsed time is a common requirement for most software development packages. It is used to determine the efficiency of the program and gives an idea of which parts of the program takes which much time. This helps in optimizing the code, such that improves its execution time. In this article, you will learn how to measure elapsed time in C++.

* high_resolution_clock::now function of the chrono header file would be used in the process. This header file may not be present in older versions of GCC.

Measuring Elapsed Time

For measuring elapsed time, two timestamps would be collected. One at the beginning of the code and the other at the end. Then we would subtract the two timestamps, and the time difference between the two would provide us with the elapsed time. 

C++




// C++ Program to demonstrate
// Measuring elapsed time
#include <chrono>
#include <iostream>
 
using namespace std::chrono;
 
int main()
{
 
    // Recording the timestamp at the start of the code
    auto beg = high_resolution_clock::now();
 
    // Actual program code goes below this
    // For demonstration a loop is ran which goes from 0 to
    // 50000
    for (int i = 0; i < 50000; i++)
        continue;
 
    // Taking a timestamp after the code is ran
    auto end = high_resolution_clock::now();
 
    // Subtracting the end timestamp from the beginning
    // And we choose to receive the difference in
    // microseconds
    auto duration = duration_cast<microseconds>(end - beg);
 
    // Displaying the elapsed time
    std::cout << "Elapsed Time: " << duration.count();
    return 0;
}


Output:

Elapsed Time: 116

Explanation: The relevant header files were imported. At the beginning of the main code, a timestamp is taken by calling the high_resolution_clock::now function and the data is stored in variable beg. After which the actual code whose elapsed time is to be measured should come. For demonstration, a loop is run from 0 to 50000 which does nothing in its body. This is done to produce an event which takes some time. After which another call to the high_resolution_clock::now function is made and the return data is stored in the variable end.

After which the difference between the end and the beg variable is obtained, and the result is cast into milliseconds precision in the statement duration_cast<microseconds>(end – beg). This gives us the time taken between the first time the clock function is called, and the last time is was called. In the end, the elapsed time is displayed using the count function.

Note: The reason why milliseconds are chosen as the precision is that most computers are capable of executing such trivial tasks (like loops) in under a second. Therefore, if the precision was chosen as seconds, we would end up with 0 as the elapsed time. Therefore to demonstrate the effect milliseconds were chosen. This could be changed to another time precision by replacing microseconds between the angular brackets with the time frame of your choice. 


Last Updated : 01 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads