top of page
Writer's pictureSunil Kumar Yadav

Understanding the C++ Chrono library


C++ Chrono library deals with date and time. This library was designed considering the fact that timers and clocks might be different on different systems. The unique property of this library is that it provides a precision-neutral concept by separating duration and point of time i.e. timepoint from the specific clock.


In order to use this library users need to #include<chrono> inside their source file. All the elements of the Chrono header are defined under the std::chrono namespace. The elements of the Chrono header deal with time using three concepts.


Duration

A duration object expresses a time span via a count like a minute, three hours or fifty milliseconds. For example, 30 seconds could be represented by a duration consisting of 30 ticks of 1 second time unit.



// 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 ms(1000); 
    ms = ms*60;
      
    std::cout << "Duration (in periods): "
              << ms.count() << " milliseconds.\n";
      
    std::cout << "Duration (in seconds): "
              << (ms.count() * milliseconds::period::num / 
                                milliseconds::period::den)<<" seconds.\n";
  
    return 0;
}


Output:


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


Clock

A clock consists of a starting point known as an epoch and a tick rate. For example, a clock may have an epoch of February 20, 1995, and tick every second. C++ defines three clock types:


system_clock

It is the current time according to the system. This is the same clock that we see in 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 continues 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.



// C++ program to illustrate time point and system clock functions
#include <iostream>
#include <chrono>
#include <ctime>

// Function to calculate Fibonacci series
long fibonacci(unsigned int n)
{
	if (n < 2) 
		return n;
	return fibonacci(n-1) + fibonacci(n-2);
}

int main()
{
  int  num=42;
  // Using time point and system_clock
  std::chrono::time_point<std::chrono::system_clock> start, end;

  start = std::chrono::system_clock::now();   // time point start 
  
  std::cout << "Fibonacci of "<< num <<" is "<< fibonacci(num) << '\n';
  
  end = std::chrono::system_clock::now();    // time point end

  // Measuring total duration to calculate and print result of Fibonacci
  std::chrono::duration<double> elapsed_seconds = end - start;  
  
  // Printing system clock
  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";
  
  return 0;
}

Output:


Fibonacci of 42 is 267914296
Finished computation at Mon Aug 16 15:18:42 2021
Elapsed time: 2.27327s


1,357 views0 comments

Yorumlar


bottom of page