Open In App

Chrono in C++

Last Updated : 20 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

<chrono> is a C++ header that provides a collection of types and functions to work with time. It is a part of the C++ Standard Template Library (STL) and it’s included in C++11 and later versions.

<chrono> provides three main types of clocks: system_clock, steady_clock, and high_resolution_clock. These clocks are used to measure time in various ways.

system_clock represents the system-wide real time wall clock. It’s affected by the system’s time adjustments.
steady_clock represents a monotonically increasing clock that’s not affected by changes to the system time.
high_resolution_clock is the clock with the shortest tick period available on the system.

<chrono> also provides a collection of duration types, including duration<Rep, Period>, that can be used to represent a duration of time. Rep is the representation type (such as int or long) and Period is the ratio of the duration (such as nanoseconds or seconds).
Additionally, <chrono> provides a collection of time point types, including time_point<Clock, Duration>, that can be used to represent a point in time. Clock is the clock type (such as system_clock) and Duration is the duration type (such as seconds)

Chrono library is used to deal with date and time. This library was designed to deal with the fact that timers and clocks might be different on different systems and thus to improve over time in terms of precision. The unique thing about chrono is that it provides a precision-neutral concept by separating duration and point of time (“timepoint”) from specific clocks. chrono is the name of a header and also of a sub-namespace: All the elements in this header (except for the common_type specializations) are not defined directly under the std namespace (like most of the standard library) but under the std::chrono namespace. The elements in this header deal with time. This is done mainly by means of three concepts:

Duration

A duration object expresses a time span by means of a count like a minute, two hours, or ten milliseconds. For example, “42 seconds” could be represented by a duration consisting of 42 ticks of a 1-second time unit. 

CPP




// C++ program to illustrate the utility
// function duration::count
#include <iostream>   
#include <chrono>    
 
int main ()
{
    using namespace std::chrono;
    // std::chrono::milliseconds is an
    // instantiation of std::chrono::duration:- 1 second
    milliseconds mil(1000);
     
    mil = mil*60;
     
    std::cout << "duration (in periods): ";
    std::cout << mil.count() << " milliseconds.\n";
     
    std::cout << "duration (in seconds): ";
    std::cout << (mil.count() * milliseconds::period::num /
                               milliseconds::period::den);
    std::cout << " seconds.\n";
 
    return 0;
}


Output:

duration (in periods): 60000 milliseconds.
duration (in seconds): 60 seconds.

Clock

A clock consists of a starting point (or epoch) and a tick rate. For example, a clock may have an epoch of February 22, 1996 and tick every second. C++ defines three clock types:

  • system_clock-It is the current time according to the system (regular clock which we see on the toolbar of the computer). It is written as- std::chrono::system_clock
  • steady_clock-It is a monotonic clock that will never be adjusted.It goes at a uniform rate. It is written as- std::chrono::steady_clock
  • high_resolution_clock– It provides the smallest possible tick period. It is written as-std::chrono::high_resolution_clock

Time point

A time_point object expresses a point in time relative to a clock’s epoch. Internally, the object stores an object of a duration type, and uses the Clock type as a reference for its epoch.  

CPP




// C++ program to illustrate time point
// and system clock functions
#include <iostream>
#include <chrono>
#include <ctime>
 
// Function to calculate
// Fibonacci series
long fibonacci(unsigned n)
{
    if (n < 2) return n;
    return fibonacci(n-1) + fibonacci(n-2);
}
 
int main()
{
    // Using time point and system_clock
    std::chrono::time_point<std::chrono::system_clock> start, end;
 
    start = std::chrono::system_clock::now();
    std::cout << "f(42) = " << fibonacci(42) << '\n';
    end = std::chrono::system_clock::now();
 
    std::chrono::duration<double> elapsed_seconds = end - start;
    std::time_t end_time = std::chrono::system_clock::to_time_t(end);
 
    std::cout << "finished computation at " << std::ctime(&end_time)
              << "elapsed time: " << elapsed_seconds.count() << "s\n";
}


Output:

f(42) = 267914296
finished computation at Wed Jan  4 05:13:48 2017
elapsed time: 2.14538s

It’s important to note that the precision and accuracy of the clocks and durations provided by <chrono> may vary depending on the system and platform, and it’s always a good idea to check the documentation of your platform for more information.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments