Chrono Library in C++ with examples
The Chrono library in C++ is utilized to work with time operations. The Chrono library has its own namespace called “chrono
“, if you want to use the chrono library first include it using #include <chrono> and then using namespace std::chrono;
There are 3 main classes in Chrono :
1. Duration: This includes time ranges, similar to 20 milliseconds, twenty hours, or twenty seconds.
2. Time Point: Reference to a particular point in time with respect to an ‘epoch’ (a fixed point in time common to all time_point objects using the same clock).
3. Clocks: This compares a point in time to the actual physical time.
Let’s understand Chrono Library in C++ with some examples:
Duration class in Chrono
We will create an object ss of the duration type seconds and use the duration_cast()
method to convert it into the duration type minutes.
Here the object ss is defined as 1200 seconds, After converting it into minutes, we should get 20 minutes as the output.
#include <iostream> #include <chrono> using namespace std; using namespace chrono; int main () { //creating an object of type seconds or std::chrono::seconds seconds ss(1200); //count() method will return no. of ticks of the duration type chosen. cout << "ss, Duration in seconds: " << ss.count() << " seconds." << endl; //The duration_cast() method converts the value of one duration type into another duration type minutes m = duration_cast<minutes>(ss); cout << "m, Duration in minutes: " << m.count() << " minutes." << endl; return 0; }
Note:
ss
is the name of the object we made of the duration type seconds.
mm
is the name of the object we made of the duration type minutes.
count()
method returns the number of ticks of the duration type chosen.
duration_cast()
method converts the value of one duration type into another duration type.
Output for the above Code
ss, Duration in seconds: 1200 seconds. m, Duration in minutes: 20 minutes.
Clocks class in Chrono library
There are three clock types in C++:
- system_clock: The time in your system’s clock is used by
system_clock
. The syntax is “chrono::system_clock
“. - steady_clock: Explicitly intended to compute time stretches. The
steady_clock
is monotonic and consistent (Every tick the clock advances takes the same amount of time). The syntax is “chrono::steady_clock()
“. - high_resolution_clock: This is the clock that has the shortest tick period. The syntax is “
high_resolution_clock
“.
Time Point
Time Point refers to a specific point in time with respect to an epoch. So we have created a custom function that will take some time to complete. So let’s measure how much time this function takes to execute with the help of Time Points.
#include <iostream> #include <chrono> #include <ctime> using namespace std; using namespace chrono; string custom_func(){ for(int i=0;i<2000;i++) for(int j=0;j<1500;j++) if(i==1999&&j==1499) return "function completed."; return "function completed."; } int main() { //The syntax is time_point<clocktype> object; time_point<system_clock> start, end; start = system_clock::now(); cout << custom_func() << endl; end = system_clock::now(); duration<double> total_seconds = end - start; time_t end_time = system_clock::to_time_t(end); std::cout << "Completed the task at " << ctime(&end_time) << "total time taken: " << total_seconds.count() << " seconds" << endl; }
Note:
The system_clock::now()
method returns the current time_point in the system_clock.
We have made the custom_func()
so that program does some computation so that it will be meaningful to record the time taken for the program to finish.
Output for the above Code
Also, see:
Leave a Reply