-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.h
45 lines (37 loc) · 1.15 KB
/
Timer.h
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
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef TIMER_H
#define TIMER_H
#include <iostream>
#include <chrono>
using namespace std::chrono;
class Timer {
public:
Timer(double duration) {
duration_ms = duration;
}
void start() {
start_time_point = high_resolution_clock::now();
running = true;
}
void stop() {
end_time_point = high_resolution_clock::now();
running = false;
}
bool times_up() const {
auto elapsed_ms = elapsedMilliseconds();
return elapsed_ms >= duration_ms;
}
double elapsedMilliseconds() const {
if (running) {
auto current_time_point = high_resolution_clock::now();
return duration<double, std::milli>(current_time_point - start_time_point).count();
} else {
return duration<double, std::milli>(end_time_point - start_time_point).count();
}
}
private:
double duration_ms;
time_point<high_resolution_clock> start_time_point;
time_point<high_resolution_clock> end_time_point;
bool running = false;
};
#endif