This post will discuss how to measure the elapsed time of a C++ program in seconds, milliseconds, microseconds, and nanoseconds using the Chrono library.

Since C++11, the best way to measure elapsed time in C++ is by using the Chrono library, which deals with time.

 
Following C++ program calculates the time elapsed for a simple code in seconds, milliseconds, microseconds, and nanoseconds. It includes the <chrono.h> header which provides access to the current time using system_clock(). The system_clock is designed to represent the real-time and used by all processes running on the system.

Download  Run Code

Output (may vary):

Elapsed time in nanoseconds: 3000090354 ns
Elapsed time in microseconds: 3000090 µs
Elapsed time in milliseconds: 3000 ms
Elapsed time in seconds: 3 sec

That’s all about measuring elapsed time of a C++ program using the Chrono library.

 
Related Post:

Find execution time of a C program