-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHETime.h
68 lines (64 loc) · 1.6 KB
/
SHETime.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
65
66
67
68
#include <chrono>
class Timer
{
private:
std::chrono::time_point<std::chrono::system_clock> m_StartTime;
std::chrono::time_point<std::chrono::system_clock> m_EndTime;
bool m_bRunning = false;
public:
void start(void) {
m_StartTime = std::chrono::system_clock::now();
m_bRunning = true;
}
void stop(void) {
m_EndTime = std::chrono::system_clock::now();
m_bRunning = false;
}
double elapsedMilliseconds(void) {
std::chrono::time_point<std::chrono::system_clock> endTime;
if (m_bRunning) {
endTime = std::chrono::system_clock::now();
} else {
endTime = m_EndTime;
}
return std::chrono::duration_cast<std::chrono::milliseconds>
(endTime-m_StartTime).count();
}
double elapsedSeconds(void) {
return elapsedMilliseconds()/1000.0;
}
};
// helper class to format the time in a human usable way
class PrintTime
{
private:
double ptime;
public:
PrintTime(double t) : ptime(t) {}
PrintTime(const PrintTime &p) : ptime(p.ptime) {}
friend std::ostream&operator<<(std::ostream&, const PrintTime &);
};
std::ostream&operator<<(std::ostream&str, const PrintTime &p) {
#define MS_TO_S 1000.0
#define MS_TO_MIN (60.0*MS_TO_S)
#define MS_TO_H (60.0*MS_TO_MIN)
#define MS_TO_D (24*MS_TO_H)
if (p.ptime < MS_TO_S) {
str << p.ptime << "ms";
return str;
}
if (p.ptime < MS_TO_MIN) {
str << (p.ptime/MS_TO_S) << "s";
return str;
}
if (p.ptime < MS_TO_H) {
str << (p.ptime/MS_TO_MIN) << "min";
return str;
}
if (p.ptime < (MS_TO_D)) {
str << (p.ptime/MS_TO_H) << "h";
return str;
}
str << (p.ptime/(MS_TO_D)) << "d";
return str;
}