-
Notifications
You must be signed in to change notification settings - Fork 0
/
PerformanceTimer.cpp
32 lines (26 loc) · 992 Bytes
/
PerformanceTimer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <chrono>
#include <iostream>
class PerformanceTimer {
public:
PerformanceTimer()
: start_timepoint(std::chrono::high_resolution_clock::now()) {}
void reset() { start_timepoint = std::chrono::high_resolution_clock::now(); }
double elapsedMilliseconds() const {
return std::chrono::duration<double, std::milli>(
std::chrono::high_resolution_clock::now() - start_timepoint)
.count();
}
double elapsedSeconds() const {
return std::chrono::duration<double>(
std::chrono::high_resolution_clock::now() - start_timepoint)
.count();
}
~PerformanceTimer() {
auto endTimepoint = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(
endTimepoint - start_timepoint);
std::cout << "Time taken: " << duration.count() << " ms." << std::endl;
}
private:
std::chrono::time_point<std::chrono::high_resolution_clock> start_timepoint;
};