Measuring elapsed time is a crucial aspect of programming, especially when it comes to optimizing and debugging code. In this tutorial, we will explore various ways to measure elapsed time in C++.
Introduction to std::chrono
The std::chrono
library provides a flexible and efficient way to work with time-related functionality in C++. It includes classes for representing time points, durations, and clocks. To use std::chrono
, you need to include the <chrono>
header file.
#include <chrono>
Measuring Elapsed Time using std::chrono::steady_clock
One of the most common ways to measure elapsed time is by using std::chrono::steady_clock
. This clock provides a monotonic clock, meaning that it always increases and never goes backwards. Here’s an example:
auto start = std::chrono::steady_clock::now();
// Code to be measured
auto end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Elapsed time: " << duration.count() << " milliseconds" << std::endl;
Using a Timer
Class
To make measuring elapsed time more convenient, you can create a Timer
class. Here’s an example implementation:
template <class DT = std::chrono::milliseconds,
class ClockT = std::chrono::steady_clock>
class Timer {
public:
void start() { start_time_ = ClockT::now(); }
void stop() { end_time_ = ClockT::now(); }
auto duration() const {
return std::chrono::duration_cast<DT>(end_time_ - start_time_);
}
private:
typename ClockT::time_point start_time_;
typename ClockT::time_point end_time_;
};
You can use this class like this:
Timer<> timer;
timer.start();
// Code to be measured
timer.stop();
auto duration = timer.duration();
std::cout << "Elapsed time: " << duration.count() << " milliseconds" << std::endl;
Instrumenting Functions for Benchmarking
You can also instrument functions for benchmarking using a measure
struct. Here’s an example implementation:
template <class TimeT = std::chrono::milliseconds,
class ClockT = std::chrono::steady_clock>
struct measure {
template <class F, class... Args>
static auto duration(F&& func, Args&&... args) {
auto start = ClockT::now();
std::invoke(std::forward<F>(func), std::forward<Args>(args)...);
return std::chrono::duration_cast<TimeT>(ClockT::now() - start);
}
};
You can use this struct like this:
auto duration = measure<>::duration(my_function, arg1, arg2);
std::cout << "Elapsed time: " << duration.count() << " milliseconds" << std::endl;
Other Methods
There are other methods for measuring elapsed time in C++, such as using clock()
from the <ctime>
header file or GetTickCount()
on Windows. However, these methods are less precise and less portable than using std::chrono
.
#include <ctime>
void f() {
clock_t begin = clock();
// Code to be measured
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
}
Conclusion
Measuring elapsed time is an essential part of programming, and C++ provides various ways to do so. The std::chrono
library offers a flexible and efficient way to work with time-related functionality, making it the recommended choice for measuring elapsed time in C++.