Chrono In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Chrono In C++

Chrono In C++

BLUF: Mastering Chrono In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Chrono In C++

C++ is renowned for its efficiency. Learn how Chrono In C++ enables low-level control and high-performance computing in the tutorial below.

Chrono serves as a C++ header comprising a collection of time-related classes and functions. This component is an integral part of the C++ Standard Template Library (STL) and has been incorporated into C++11 and following iterations.

<Chrono> supports three sorts of clocks: systemclock, steadyclock , and highresolutionclock . These clocks are utilized for measuring time in a variety of ways.

  • system_clock is the system-wide real-time wall clock. The system's time adjustments influence it.
  • steady_clock represents a monotonically growing clock that is not impacted by modifications to the system time.
  • highresolutionclock is the system's internal clock with the smallest tick period.

<chrono> consists of different types of time durations, like duration <Rep, Period> , which are employed to specify a length of time. Rep signifies the data type (like int or long), while Period indicates the time unit (such as microseconds or seconds).

Furthermore, <chrono> consists of a variety of time point categories, like timepointClock, Duration>, which can be employed to define a specific moment in time. The clock represents the type of clock (e.g., systemclock), while Duration signifies the duration type (e.g., seconds).

The Chrono library serves the purpose of handling dates and times efficiently. Its development aimed to address the variability in timers and clocks across different systems, enhancing precision consistently. One notable aspect of chrono is its ability to offer a precision-agnostic approach by separating duration and time point ("timepoint") from specific clocks. The term chrono is utilized for both a header and a sub-namespace: With the exception of the common_type specializations, all the elements within this header are situated in the std::chrono namespace rather than the std namespace (unlike most components of the standard library). The components in this header primarily focus on time-related functionalities, predominantly revolving around three key concepts.

Duration

A duration object signifies a length of time by specifying a quantity like a minute, a couple of hours, or even ten milliseconds. For instance, "48seconds" could be depicted as 48 increments within a 1-second interval.

Filename: Duration_count.c

Example

// Program for using Chrono header file
#include <iostream> 
#include <chrono>	 
int main ()
{
	using namespace std::chrono;

	milliseconds milli(1000); 
	
	milli = milli*60;
	
	std::cout << "The duration (in periods): ";
	std::cout << milli.count() << " the time in milliseconds.\n";
	
	std::cout << "The duration (in seconds): ";
	std::cout << (milli.count() * milliseconds::period::num / 
							milliseconds::period::den);
	std::cout << " seconds.\n";

	return 0;
}

Output:

Output

The duration (in periods): 60000 the time in milliseconds.
The duration (in seconds): 60 seconds.

Clock

A clock comprises a beginning point (epoch) and a tick rate. For example, a clock could have an epoch of February 22, 1997 , tick every second. C++ defines three types of clocks:

  • systemclock- It is the time at the moment according to the system (the ordinary clock we see on the system's toolbar). The syntax is std::chrono::systemclock .
  • steadyclock- It's a static clock that will never be altered. It moves at a consistent rate. The syntax is std::chrono::steadyclock .
  • highresolutionclock- It has the shortest tick period conceivable. It's formatted as- std::chrono::highresolutionclock .
  • Point in time

A time_point instance symbolizes a specific moment in time in relation to the starting point of a clock. Within, the instance holds a duration object that is associated with the Clock type.

Filename: Systemclock.c

Example

// C++ application describe time point // and system clock methods
#include <iostream>
#include <chrono>
#include <ctime>

// function for calculating the Fibonacci series
long fib(unsigned num)
{
	if (num < 2) return num;
	return fib(num-1) + fib(num-2);
}

int main()
{
	
	std::chrono::time_point<std::chrono::system_clock> start, end;

	start = std::chrono::system_clock::now();
	std::cout << "f(45) = " << fib(45) << '\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 << "The finished computation is at " << std::ctime(&end_time)
			<< "The elapsed time is: " << elapsed_seconds.count() << "s\n";
}

Output:

Output

f(45) = 1134903170
The finished computation is at Mon Oct 9 16:15:36 2023
The elapsed time is 9.89949s

It is important to mention that the exactness and correctness of the timepieces and time spans provided by <chrono> might differ depending on the system and platform; hence, refer to your platform's specifications for further details.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience