In many software applications , precisely measuring execution time is essential, particularly when optimizing Code or evaluating the performance of various algorithms.
In order to measure execution time in C++ precisely, time intervals must be captured and computed using the <chrono> library introduced in C++11. A collection of clock types and time-related utilities are available in the <chrono> library, which is intended to be more accurate and portable than conventional C-style time functions. There are several function that are utilized to measure execution time with high precision in C++.
1. Clock Types
The <chrono> library presents several clock types, including std::chrono::steadyclock, std::chrono::highresolutionclock, and std::chrono::systemclock. Because it offers the maximum precision on the system, highresolutionclock is frequently chosen for measuring execution time.
2. Time Points
Clock functions are used to obtain time points and discrete moments in time. In order to find out how long a code segment takes to execute, we take note of its start and end times.
auto start_time = std::chrono::high_resolution_clock::now();
//Code to measure goes here
auto end_time = std::chrono::high_resolution_clock::now();
3. Duration
The difference between two points in time is represented as a duration. They compute the elapsed time between the code segment’s beginning and conclusion.
4. Units of Duration
Depending on our requirements, we can select a duration unit such as milliseconds, seconds, or microseconds.
auto duration_micro = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
auto duration_milli = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);
auto duration_sec = std::chrono::duration_cast<std::chrono::seconds>(end_time - start_time);
5. Precision and Portability
Though it usually measures in nanoseconds or less, the precision of <chrono> is implementation-defined.
Because of its cross-platform and cross-compiler compatibility, the library operates consistently.
6. Advantages:
- Precision: Measurements can be made more accurately because <chrono> is more precise than conventional C time functions.
- Portability: The library can measure execution time on various systems because of its platform-independent nature.
- Consistency: By guaranteeing consistent behaviour across platforms and compilers, time measurement inconsistencies are less likely.
- Ease of Use: Integrating high-precision timing into our C++ code is reasonably accessible because of the simple syntax.
- Performance Profiling: Locate bottlenecks and enhance crucial code segments.
- Algorithm Comparison: Compare the efficiency of different algorithms by measuring their execution times.
- Real-time systems: Make sure that, in real-time applications, specific processes are completed within predetermined time limits.
7. Use Cases:
Example:
Let us take an example to illustrate how to measure execution time with high precision in C++.
#include <iostream>
#include <chrono>
int main()
{
// Capture the start time point
auto start_time = std::chrono::high_resolution_clock::now();
//Code to measure execution time goes here
for (int i = 0; i < 1000000; ++i)
{
// Some computation-intensive operation
int result = i * i;
}
// Capture the end time point
auto end_time = std::chrono::high_resolution_clock::now();
// Calculate the duration between start and end time points
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
// Print the execution time in microseconds
std::cout << "Execution Time: " << duration.count() << " microseconds" << std::endl;
return 0;
}
Output:
Execution Time: 1152 microseconds
Explanation:
- Include Required Headers
The input/output operations (<iostream>) and high-precision timing functionality (<chrono>) require the headers that are included in these lines.
- Capture Start Time
In this case, the current time point is obtained using std::chrono::highresolutionclock::now and saved in the variable start_time. The execution time can be calculated from this point forward.
- Code to Measure Time of Execution
This loop simulates a computationally demanding process. It is simply a stand-in for the code we want to evaluate. Therefore, the specifics of the operation are not essential to know when calculating execution time.
- Capture End Time
Like the start time, this line captures the current time in the variable end_time. It is the point at which the code block whose execution duration we need to gauge ends.
- Calculate Duration
The std::chrono::durationcast method is used to find the time between starttime and end_time. After that, it is cast to microseconds to make the resultant duration easier to read.
- Print Execution Time
Ultimately, the execution time computation is output to the console. The duration is retrieved in microseconds using duration.count.
- Return 0
In order to notify the operating system that the program has been executed successfully, the main function returns 0.
Conclusion:
In conclusion, the code records a computationally demanding task’s beginning and ending times, determines the time interval between them, and outputs the execution time in microseconds to the console. It offers a basic framework that we can modify to gauge how long particular C++ program segments take to execute.