The <chrono> header, introduced in C++11, provides classes and functions for measuring time, representing durations, and working with clocks. It offers a portable and precise way to perform time-related operations in C++ programs.
- Provides clocks, durations, and time points for time measurement.
- Supports high-precision and platform-independent time operations.
Example: The following example measures the execution time of a piece of code using std::chrono.
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
int main() {
auto start = high_resolution_clock::now();
// Code to measure
for (int i = 0; i < 1000000; i++);
auto end = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(end - start);
cout << "Execution Time: "
<< duration.count()
<< " microseconds";
}
Output
Execution Time: 2212 microseconds
Explanation: The program records the start and end time of a code segment and calculates the elapsed time using the <chrono> library.
Main Components of <chrono>
The <chrono> library is built around three fundamental concepts:
Duration
A duration represents the amount of time between two events. It stores both the numeric value and the unit of time.
- Represented using std::chrono::duration.
- Can store seconds, milliseconds, microseconds, nanoseconds, and more.
- Supports arithmetic operations such as addition and subtraction.
- The count() function returns the stored value.
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
int main()
{
seconds s(5);
milliseconds ms = duration_cast<milliseconds>(s);
cout << "Seconds: " << s.count() << endl;
cout << "Milliseconds: " << ms.count() << endl;
return 0;
}
Output
Seconds: 5 Milliseconds: 5000
Explanation: A duration object stores a time interval. The count() function returns the numeric value of the duration, while duration_cast converts one duration type to another.
Clock
A clock provides the current time and acts as the reference for measuring durations and creating time points. C++ provides three standard clock types:
| Clock | Description |
|---|---|
| system_clock | Represents the system's real-time clock. |
| steady_clock | Monotonic clock that is never adjusted and is ideal for measuring elapsed time. |
| high_resolution_clock | Provides the smallest available tick period for high-precision measurements. |
Time Point
A time point represents a specific moment in time relative to the epoch of a clock.
- Represented using std::chrono::time_point.
- Obtained using the now() function of a clock.
- Can be subtracted to calculate elapsed time.
- Frequently used for benchmarking program execution.
Example: The following program demonstrates the use of time_point and system_clock.
#include <chrono>
#include <iostream>
#include <thread>
using namespace std;
using namespace std::chrono;
int main()
{
time_point<steady_clock> start = steady_clock::now();
// Simulate some work
this_thread::sleep_for(seconds(2));
time_point<steady_clock> end = steady_clock::now();
auto elapsed = duration_cast<milliseconds>(end - start);
cout << "Elapsed Time: "
<< elapsed.count() << " ms";
return 0;
}
Output
Elapsed Time: 2000 ms
Explanation: The program records the start and end time of the computation, then subtracts the two time points to determine the execution time.
Common Duration Types
The <chrono> library provides several predefined duration types.
| Duration Type | Time Unit |
|---|---|
| nanoseconds | 10⁻⁹ second |
| microseconds | 10⁻⁶ second |
| milliseconds | 10⁻³ second |
| seconds | 1 second |
| minutes | 60 seconds |
| hours | 60 minutes |
Advantages of <chrono>
The <chrono> library provides several benefits:
- Works consistently across different platforms.
- Separates clocks, durations, and time points for better flexibility.
- Simplifies benchmarking and performance analysis.
Limitations of <chrono>
Despite its advantages, <chrono> has some limitations:
- Clock precision depends on the underlying operating system.
- Different clocks serve different purposes and should be chosen carefully.
- Some advanced time-zone features require newer C++ standards.