-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonotonic_timer.cc
More file actions
114 lines (91 loc) · 3.02 KB
/
monotonic_timer.cc
File metadata and controls
114 lines (91 loc) · 3.02 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "monotonic_timer.h"
time_t MonotonicTimer::GetSec() const {
return timer.tv_sec;
}
long long MonotonicTimer::GetNSec() const {
return timer.tv_sec * 1e9 + timer.tv_nsec;
}
double MonotonicTimer::GetMSec() const {
double msec = double(timer.tv_nsec)/double(1e6);
return timer.tv_sec * 1e3 + msec;
}
bool MonotonicTimer::operator==(const MonotonicTimer& t) const {
return (timer.tv_sec == t.timer.tv_sec && timer.tv_nsec == t.timer.tv_nsec);
}
bool MonotonicTimer::operator<(const MonotonicTimer& t) const {
if(timer.tv_sec < t.timer.tv_sec)
return true;
else if(timer.tv_sec == t.timer.tv_sec && timer.tv_nsec < t.timer.tv_nsec)
return true;
else
return false;
}
bool MonotonicTimer::operator>(const MonotonicTimer& t) const {
if(timer.tv_sec > t.timer.tv_sec)
return true;
else if(timer.tv_sec == t.timer.tv_sec && timer.tv_nsec > t.timer.tv_nsec)
return true;
else
return false;
}
bool MonotonicTimer::operator>=(const MonotonicTimer& t) const {
if(timer.tv_sec > t.timer.tv_sec)
return true;
else if(timer.tv_sec == t.timer.tv_sec && timer.tv_nsec >= t.timer.tv_nsec)
return true;
else
return false;
}
bool MonotonicTimer::operator<=(const MonotonicTimer& t) const {
if(timer.tv_sec < t.timer.tv_sec)
return true;
else if(timer.tv_sec == t.timer.tv_sec && timer.tv_nsec <= t.timer.tv_nsec)
return true;
else
return false;
}
MonotonicTimer& MonotonicTimer::operator=(const MonotonicTimer& t) {
if(this == &t)
return *this;
this->timer = {t.timer.tv_sec, t.timer.tv_nsec};
return *this;
}
MonotonicTimer& MonotonicTimer::operator-=(const MonotonicTimer& t) {
if(*this < t)
cout<<"In monotonic_timer.cc: (operator-=)"<<"right value is larger than left value"<<endl;
//assert(!(*this < t));
this->timer.tv_sec-=t.timer.tv_sec;
this->timer.tv_nsec-=t.timer.tv_nsec;
if(this->timer.tv_nsec < 0)
this->timer.tv_sec-=1, this->timer.tv_nsec+=BILLION;
return *this;
}
MonotonicTimer& MonotonicTimer::operator+=(const MonotonicTimer& t) {
this->timer.tv_sec+=t.timer.tv_sec;
this->timer.tv_nsec+=t.timer.tv_nsec;
if(this->timer.tv_nsec >= BILLION)
this->timer.tv_sec+=1, this->timer.tv_nsec-=BILLION;
return *this;
}
const MonotonicTimer MonotonicTimer::operator+ (const MonotonicTimer& t1) const {
return MonotonicTimer(*this)+=t1;
}
const MonotonicTimer MonotonicTimer::operator- (const MonotonicTimer& t1) const {
//assert(!(*this < t1));
if(*this < t1)
cout<<"In monotonic_timer.cc: (operator-)"<<"left is larger than right!"<<endl;
return MonotonicTimer(*this)-=t1;
}
MonotonicTimer& MonotonicTimer::operator/=(const int32_t n) {
this->timer.tv_nsec = this->timer.tv_nsec/n + (this->timer.tv_sec%n)*(BILLION/n);
this->timer.tv_sec = this->timer.tv_sec/n;
return *this;
}
const MonotonicTimer MonotonicTimer::operator/ (const int32_t n) const {
return MonotonicTimer(*this)/=n;
}
void MonotonicTimer::PrintTimer(bool line/* = false*/) {
cout<<this->timer.tv_sec<<","<<this->timer.tv_nsec;
if(line)
cout<<endl;
}