Measuring the execution time of a function is useful for analyzing performance and comparing different implementations. In modern C++, the <chrono> library introduced in C++11 provides a precise and portable way to measure execution time.
- The <chrono> library provides clocks, time points, and durations for time measurement.
- Execution time is measured by recording the start and end time points and calculating the difference between them.
Example: Measuring Execution Time of sort()
#include <algorithm>
#include <chrono>
#include <iostream>
#include <vector>
using namespace std;
using namespace std::chrono;
int main()
{
vector<int> values(10000);
auto f = []() {
return rand() % 10000;
};
generate(values.begin(),
values.end(), f);
// Record starting time
auto start =
high_resolution_clock::now();
// Function whose execution
// time is to be measured
sort(values.begin(),
values.end());
// Record ending time
auto stop =
high_resolution_clock::now();
auto duration =
duration_cast<microseconds>(
stop - start);
cout << "Time taken: "
<< duration.count()
<< " microseconds";
return 0;
}
Output
Time taken: 3548 microseconds
Explanation
- The program records the start and end time of the sort() operation.
- duration_cast() converts the elapsed time into microseconds.
- count() returns the execution time value.
Working of <chrono>
The <chrono> library provides the following components for measuring execution time:
- Clock: Generates the current time.
- Time Point: Represents a specific point in time.
- Duration: Represents the time interval between two time points.
Among the available clocks, std::chrono::high_resolution_clock is commonly used for performance measurement because it provides the highest available precision.
Steps to Measure Execution Time
To measure the execution time of a function:
- Record the starting time using high_resolution_clock::now().
- Execute the function.
- Record the ending time.
- Compute the difference between the two time points using duration_cast().
Syntax
auto start = std::chrono::high_resolution_clock::now();
// Function call
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast< std::chrono::microseconds>(
stop - start);
std::cout << duration.count();
Common Time Units in <chrono>
The <chrono> library provides several predefined duration units:
| Unit | Description |
|---|---|
| nanoseconds | Billionths of a second |
| microseconds | Millionths of a second |
| milliseconds | Thousandths of a second |
| seconds | Seconds |
| minutes | Minutes |
| hours | Hours |
Advantages of Using <chrono>
The <chrono> library provides a modern and type-safe way to measure execution time.
- Provides high-precision timing for accurate performance measurement.
- Supports multiple predefined time units such as microseconds and milliseconds.
- Offers portable and standard-compliant timing utilities across platforms.
- Eliminates the need for platform-specific timing functions and APIs.
Best Practices
The following practices help obtain reliable execution measurements:
- Prefer std::chrono::high_resolution_clock for performance measurements.
- Measure only the code segment whose execution time is required.
- Execute the code multiple times and average the results for accurate benchmarking.
- Avoid including input/output operations while measuring performance.