-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTimer.h
64 lines (40 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef TIMER_H
#define TIMER_H
#include <chrono>
#include <thread>
class Timer {
//Microseconds
std::chrono::steady_clock::time_point begin;
std::chrono::steady_clock::time_point end;
//Seconds
std::chrono::high_resolution_clock::time_point begin_s;
std::chrono::high_resolution_clock::time_point end_s;
public:
Timer() { }
double elapsed(char type='s') {
switch (type) {
case 'u': //Microseconds
return std::chrono::duration_cast<std::chrono::microseconds>(end-begin).count();
break;
case 'm': //Milliseconds
return std::chrono::duration_cast<std::chrono::milliseconds>(end-begin).count();
break;
default: //Seconds
return std::chrono::duration_cast<std::chrono::seconds>(end-begin).count();
break;
}
}
void start() {
begin = std::chrono::steady_clock::now();
return;
}
double stop(char type='s') {
end = std::chrono::steady_clock::now();
return elapsed(type);
}
void sleep(int s) {
std::this_thread::sleep_for(s);
return;
}
};
#endif