Chrono in C++
<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:
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.
duration (in periods): 60000 milliseconds. duration (in seconds): 60 seconds.
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
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.
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.
This article is contributed by Shambhavi Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...