Chrono Library in C++



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

Why We Need <chrono>

It provides a precision-neutral concept by separating the durations and points of time. So, if we want to improve time over precision, we can use this library.

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

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

<chrono> provides a collection of duration types. That includes duration<Rep, Period>, which can be used to represent a duration of time. Rep represents the type (such as int or long), and Period represents the ratio of the duration (such as nanosecond or second).

<chrono> provides collection of time point types, such as time_point<Clock, Duration>, for representing a specific point in time. Clock is the clock type (such as system_clock), while Duration is the duration type (such as seconds).

Example to use std::chrono Library

In the following C++ example, we demonstrate how to use the std::chrono library to represent and manipulate time durations. We create 1000 milliseconds, multiply it by 60 to represent a full minute, and then convert and display the result in both milliseconds and seconds using duration_cast.

#include <iostream>
#include <chrono>

using namespace std;
using namespace std::chrono;

int main() {
   // Declare duration in milliseconds
   milliseconds mili(1000); // 1000 milliseconds = 1 second
   
   // Multiply by 60 to get 60 seconds worth of milliseconds
   mili = mili * 60;
   cout << "Duration : ";
   cout << mili.count() << " milliseconds.\n";
   
   // Convert milliseconds to seconds using duration_cast
   seconds sec = duration_cast<seconds>(mili);
   cout << "Duration : ";
   cout << sec.count() << " seconds.\n";
   return 0;
}

Following is the output of the code -

Duration : 60000 milliseconds.
Duration : 60 seconds.

Example of Time-point and System Clock Function

This example demonstrates the time-point and system clock function: A time_point object represents a specific moment in time, based on a clock. Internally, it stores a duration that shows how much time has passed since the clock's starting point, called the epoch.

#include <iostream>
#include <chrono>
#include <ctime>

using namespace std;
using namespace std::chrono;

// Function to calculate power (base^exponent)
long long power(int base, int exponent) {
   long long result = 1;
   for (int i = 0; i < exponent; ++i)
      result *= base;
   return result;
}

int main() {
   int base = 5, exponent = 3;

   // Start time
   time_point < system_clock > start = system_clock::now();

   // Perform power calculation
   long long result = power(base, exponent);
   cout << base << "^" << exponent << " = " << result << '\n';

   // End time
   time_point < system_clock > end = system_clock::now();

   // Calculate elapsed time in seconds
   duration < double > elapsed_seconds = end - start;

   // Convert end time to readable format
   time_t end_time = system_clock::to_time_t(end);

   // Output results
   cout << "Finished computation at " << ctime( & end_time);
   cout << "Elapsed time: " << elapsed_seconds.count() << "s\n";

   return 0;
}

Following is the output -

5^3 = 125
Finished computation at Tue May 13 11:21:37 2025
Elapsed time: 3.688e-05s
Updated on: 2025-05-15T15:38:25+05:30

671 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements