Measure elapsed time of a C++ program using chrono library

In this post, we will discuss how to measure elapsed time of a C++ program in seconds, milliseconds, microseconds and nanoseconds using chrono library.


 

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

 
Below 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

 
Related Post: Find the execution time of a C program

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz