The <chrono>
library is part of the C++ date and time utilities. <chrono>
is also the name of a namespace, so all objects defined inside the library are under the std::chrono
namespace instead of the standard namespace.
The <chrono>
header provides a precision-neutral way of handling date and time that is independent of the underlying frameworks used by different systems.
The remarkable thing about <chrono>
is that it gives a precision-neutral concept by isolating duration
and timepoint
from particular clocks
.
There are three main concepts related to the <chrono>
header.
duration
represents a time span between two timepoints
, where each tick represents a
#include <iostream>#include <chrono>using namespace std;int main (){using namespace std::chrono;microseconds micro(1000);micro = micro / 1000;cout << "duration (in periods): ";cout << micro.count() << " millisecond." << endl;cout << "duration (in seconds): ";cout << ((float)micro.count() * microseconds::period::num / microseconds::period::den);cout << " seconds.\n";return 0;}
The code above demonstrates the use of duration
. microseconds
is a helper type that represents a duration
in microseconds.
We can print the duration in milliseconds using the following code snippet.
micro = micro / 1000;
microseconds::period::num
and microseconds::period::den
represent the numerator and the denominator of the conversion factor that will convert the given value into seconds. We can cast these values into float
to get a non-zero answer as all these constants and values are of type int
by default.
A clock consists of a tick rate from a
There are three types of clocks defined in the C++ <chrono>
header.
system_clock
: Gives the current system time, which represents the system-wide real-time wall clock. It does not count leap seconds elapsed since the epoch. system_clock
is not monotonic and can be adjusted at any time.std::chrono::system_clock
steady_clock
: A monotonic clock that cannot be adjusted. steady_clock
ticks at a uniform rate.std::chrono::steady_clock
high_resolution_clock
: Provides the smallest tick period implementation.std::chrono::high_resolution_clock
We can use the clock to measure the performance of a block of code, as shown below.
#include <iostream>#include <chrono>#include <unistd.h>using namespace std;void someFunction(){sleep(1);}int main (){using namespace std::chrono;auto time1 = high_resolution_clock::now();someFunction();auto time2 = high_resolution_clock::now();auto duration = duration_cast<seconds>(time2 - time1);cout << duration.count() << " second(s)"<< endl;return 0;}
The code above measures the time it takes for someFunction()
to execute.
We use high_resolution_clock::now()
to store the starting time and the ending time of someFunction()
.
The duration_cast()
function is used to convert the duration from one type to another. In this case, we convert the time elapsed into seconds
and print that out to the screen.
The timepoint
class represents a point in time. timepoint
holds a value of type duration
and uses the clock
type as a reference for the epoch.
#include <iostream>#include <chrono>#include <unistd.h>using namespace std;void someFunction(unsigned int time){sleep(time);}int main(){using namespace std::chrono;time_point<system_clock> begin, end;begin = system_clock::now();someFunction(5);end = system_clock::now();duration<double> duration = end - begin;cout << "elapsed time: " << duration.count() << "s\n";}
Here, we use variables of type time_point
to store the time returned by system_clock::now()
. We then use the begin
and end
variables to calculate the time elapsed during the execution of someFunction()
.
Free Resources