Chrono Library in Modern C++

Hello, everyone. In this article, we are going to briefly talk about the Chrono library in the standard template library.

First of all, in order to use the Chrono library, we first need to include the header chrono to our code. This header includes all the main methods related to time.

#include <chrono>

In general, there are two important concepts related to the Chrono library: clock and duration. You can easily guess that the clock represents the time point and the duration represents the time interval:

1. std::chorono::system_clock — represents the system-wide real-time wall clock. (not steady because for most of the systems, user can adjust the time anytime, so system_clock is not monotonic).
2. std::chorono::steady_clock — goes at a uniform rate (monotonic).
3. std::chorono::high_resolution_clock — provides smallest possible tick period.

std::chrono::duration<class rep, class period = std::ratio<1>>: represents time duration. The first parameter defines the data type we want to save our time, the second parameter defines the ratio for current time. For example:
duration<int, ratio<1, 1>>:  //number of seconds stored in a int
duration<double, ratio<60,1>>  //number of minutes stored in a double

std::chrono::system_clock::duration is shortcut of std::chrono::duration<T, system clock::period>. By default, the std::chrono::system_clock::duration is equivalent to std::chrono::duration<long long, std::ratio_multiply<std::hecto, std::nano>>.

std::chrono::time_point<>: represents a point of time.

std::chrono::time_point<system_clock, millisecond>: according to system_clock, the elapsed time since epoch in milliseconds. (00:00 January 1st, 1970 (coordinated Universal Time – UTC) – the epoch of a clock)

We can also use some built-in predefined time units: nanoseconds, microseconds, milliseconds, seconds, minutes, hours…

system_clock::time_point is a shortcut of time_point <system_clock, system_clock::duration>;  steady_clock::time_point is a shortcut of time_point <steady_clock, steady_clock::duration>

The following code provides some reference for how to use the chorono library. You can check the comment and get the idea:

int main(){
std::ratio<1, 10> r; //1/10
cout << r.num << "/" << r.den <<endl;
//ALl clock frequency is represented by a ratio, we can print the
//ratio using the similar approach.
cout << chrono::system_clock::period::num <<"/" <<chrono::steady_clock::period::den <<endl;
chrono::microseconds mi(2700);//2700 microseconds!
mi.count(); //get the value 2700 here
//2700000 nanoseconds. The coverssion works fine here for we are converting
//low resolution to high resolution
chrono::nanoseconds na = mi;
na.count(); //get the value 2700000 here
//We need to do the cast here if we want to convert high resolution
//to low resolution. 2 milliseconds, the value is truncated.
chrono::milliseconds mill = chrono::duration_cast<chrono::milliseconds>(mi);
mi = mill + mi; //4700
//Time point//
//current time of system clock
chrono::system_clock::time_point tp = chrono::system_clock::now();
cout << tp.time_since_epoch().count() << endl;
tp = tp + chrono::seconds(2);
cout << tp.time_since_epoch().count() << endl;
//Measure the time span
chrono::steady_clock::time_point start = chrono::steady_clock::now();
cout <<"I am bored!!!" <<endl;
chrono::steady_clock::time_point end = chrono::steady_clock::now();
chrono::steady_clock::duration d = end – start;
if(d == chrono::steady_clock::duration::zero()){
cout << "No time elapsed!" <<endl;
cout << chrono::duration_cast<chrono::microseconds>(d).count() <<endl;
return 0;

view raw


hosted with ❤ by GitHub

That’s all for how to use the chorono library. Please give it a try and see what you can get.  Thank you for reading.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s