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

 
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)

Loading...

Thanks for reading.

Please use our online compiler to post code in comments. To contribute, get in touch with us.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
newest oldest most voted
Notify of
111
Guest

yaaaaaa