forked from davheld/precision-tracking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhigh_res_timer.cpp
More file actions
132 lines (106 loc) · 2.49 KB
/
Copy pathhigh_res_timer.cpp
File metadata and controls
132 lines (106 loc) · 2.49 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
* high_res_timer.cpp
*
* Author: Alex Teichman
*
*/
#include <precision_tracking/high_res_timer.h>
namespace precision_tracking {
HighResTimer::HighResTimer(const std::string& description,
const clockid_t& clock)
: description_(description),
total_us_(0),
clock_(clock)
{
}
void HighResTimer::start()
{
clock_gettime(clock_, &start_);
}
void HighResTimer::stop()
{
clock_gettime(clock_, &end_);
total_us_ += 1e6 * (end_.tv_sec - start_.tv_sec) + 1e-3 * (end_.tv_nsec - start_.tv_nsec);
}
void HighResTimer::reset(const std::string& description)
{
description_ = description;
total_us_ = 0;
}
void HighResTimer::reset()
{
total_us_ = 0;
}
double HighResTimer::getMicroseconds() const
{
return total_us_;
}
double HighResTimer::getMilliseconds() const
{
return getMicroseconds() / 1000.;
}
double HighResTimer::getSeconds() const
{
return getMilliseconds() / 1000.;
}
double HighResTimer::getMinutes() const
{
return getSeconds() / 60.;
}
double HighResTimer::getHours() const
{
return getMinutes() / 60.;
}
std::string HighResTimer::reportMicroseconds() const
{
std::ostringstream oss; oss << description_ << ": " << getMicroseconds() << " microseconds.";
return oss.str();
}
std::string HighResTimer::reportMilliseconds() const
{
std::ostringstream oss; oss << description_ << ": " << getMilliseconds() << " milliseconds.";
return oss.str();
}
std::string HighResTimer::reportSeconds() const
{
std::ostringstream oss; oss << description_ << ": " << getSeconds() << " seconds.";
return oss.str();
}
std::string HighResTimer::reportMinutes() const
{
std::ostringstream oss; oss << description_ << ": " << getMinutes() << " minutes.";
return oss.str();
}
std::string HighResTimer::reportHours() const
{
std::ostringstream oss; oss << description_ << ": " << getHours() << " hours.";
return oss.str();
}
std::string HighResTimer::report() const
{
double val = getMicroseconds();
if(val <= 1000.0)
return reportMicroseconds();
val /= 1000.0;
if(val <= 1000.0 && val >= 1.0)
return reportMilliseconds();
val /= 1000.0;
if(val <= 60.0 && val >= 1.0)
return reportSeconds();
val /= 60.0;
if(val <= 60.0 && val >= 1.0)
return reportMinutes();
val /= 60.0;
return reportHours();
}
ScopedTimer::ScopedTimer(const std::string& description) :
hrt_(description)
{
hrt_.start();
}
ScopedTimer::~ScopedTimer()
{
hrt_.stop();
std::cout << hrt_.report() << std::endl;
}
} // namespace precision_tracking