g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
stuff
tictoc.cpp
Go to the documentation of this file.
1
// g2o - General Graph Optimization
2
// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3
// All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions are
7
// met:
8
//
9
// * Redistributions of source code must retain the above copyright notice,
10
// this list of conditions and the following disclaimer.
11
// * Redistributions in binary form must reproduce the above copyright
12
// notice, this list of conditions and the following disclaimer in the
13
// documentation and/or other materials provided with the distribution.
14
//
15
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27
#include "
tictoc.h
"
28
29
#include <algorithm>
30
#include <cstdio>
31
#include <cstdlib>
32
#include <limits>
33
#include <map>
34
#include <vector>
35
36
#include "
misc.h
"
37
#include "
timeutil.h
"
38
39
namespace
g2o
{
40
44
struct
TicTocElement
{
45
double
ticTime
;
46
double
totalTime
;
47
int
numCalls
;
48
double
minTime
;
49
double
maxTime
;
50
double
exponentialMovingAverage
;
52
std::string
algorithmPart
;
53
bool
clockIsRunning
;
54
TicTocElement
()
55
:
ticTime
(0.),
56
totalTime
(0.),
57
numCalls
(0),
58
minTime
(
std
::numeric_limits<double>::max()),
59
maxTime
(0.),
60
exponentialMovingAverage
(0.),
61
clockIsRunning
(true) {}
62
bool
operator<
(
const
TicTocElement
& other)
const
{
63
return
totalTime
< other.
totalTime
;
64
}
65
};
66
typedef
std::map<std::string, TicTocElement>
TicTocMap
;
67
72
struct
TicTocInitializer
{
73
TicTocMap
tictocElements
;
74
bool
enabled
;
75
TicTocInitializer
() {
enabled
= getenv(
"G2O_ENABLE_TICTOC"
) != NULL; }
76
~TicTocInitializer
() {
77
if
(!
enabled
) {
78
return
;
79
}
80
81
if
(
tictocElements
.size() > 0) {
82
int
longestName = 0;
83
// sort the elements according to the total time and print a table
84
std::vector<TicTocElement> sortedElements;
85
sortedElements.reserve(
tictocElements
.size());
86
for
(TicTocMap::const_iterator it =
tictocElements
.begin();
87
it !=
tictocElements
.end(); ++it) {
88
if
(it->second.numCalls == 0)
continue
;
89
longestName = std::max(longestName, (
int
)it->first.size());
90
sortedElements.push_back(it->second);
91
}
92
std::sort(sortedElements.begin(), sortedElements.end());
93
94
longestName += 4;
95
96
// now print the table to stdout
97
printf(
"------------------------------------------\n"
);
98
printf(
"| TICTOC STATISTICS |\n"
);
99
printf(
"------------------------------------------\n"
);
100
for
(std::vector<TicTocElement>::const_iterator it =
101
sortedElements.begin();
102
it != sortedElements.end(); ++it) {
103
double
avgTime = it->totalTime / it->numCalls;
104
printf(
"%s"
, it->algorithmPart.c_str());
105
for
(
int
i = it->algorithmPart.size(); i < longestName; ++i)
106
putchar(
' '
);
107
printf(
108
"numCalls= %d\t total= %.4f\t avg= %.4f\t min= %.4f\t max= %.4f\t "
109
"ema= %.4f\n"
,
110
it->numCalls, it->totalTime, avgTime, it->minTime, it->maxTime,
111
it->exponentialMovingAverage);
112
}
113
printf(
"------------------------------------------\n"
);
114
}
115
}
116
};
117
118
double
tictoc
(
const
char
* algorithmPart) {
119
static
TicTocInitializer
initializer;
120
if
(!initializer.
enabled
)
return
0.;
121
122
TicTocMap
& tictocElements = initializer.
tictocElements
;
123
static
double
alpha =
cst
(0.01);
124
double
now =
get_monotonic_time
();
125
126
double
dt = 0.;
127
TicTocMap::iterator foundIt = tictocElements.find(algorithmPart);
128
if
(foundIt == tictocElements.end()) {
129
// insert element
130
TicTocElement
e;
131
e.
ticTime
= now;
132
e.
algorithmPart
= algorithmPart;
133
tictocElements[e.
algorithmPart
] = e;
134
}
else
{
135
if
(foundIt->second.clockIsRunning) {
136
dt = now - foundIt->second.ticTime;
137
foundIt->second.totalTime += dt;
138
foundIt->second.minTime = std::min(foundIt->second.minTime, dt);
139
foundIt->second.maxTime = std::max(foundIt->second.maxTime, dt);
140
if
(foundIt->second.numCalls == 0)
141
foundIt->second.exponentialMovingAverage = dt;
142
else
143
foundIt->second.exponentialMovingAverage =
144
(1. - alpha) * foundIt->second.exponentialMovingAverage +
145
alpha * dt;
146
foundIt->second.numCalls++;
147
}
else
{
148
foundIt->second.ticTime = now;
149
}
150
foundIt->second.clockIsRunning = !foundIt->second.clockIsRunning;
151
}
152
return
dt;
153
}
154
155
ScopedTictoc::ScopedTictoc
(
const
char
* algorithmPart)
156
:
_algorithmPart
(algorithmPart) {
157
tictoc
(
_algorithmPart
.c_str());
158
}
159
160
ScopedTictoc::~ScopedTictoc
() {
tictoc
(
_algorithmPart
.c_str()); }
161
162
}
// namespace g2o
g2o::ScopedTictoc::_algorithmPart
std::string _algorithmPart
Definition
tictoc.h:65
g2o::ScopedTictoc::~ScopedTictoc
~ScopedTictoc()
Definition
tictoc.cpp:160
g2o::ScopedTictoc::ScopedTictoc
ScopedTictoc(const char *algorithmPart)
Definition
tictoc.cpp:155
misc.h
some general case utility functions
g2o
Definition
dl_wrapper.cpp:55
g2o::TicTocMap
std::map< std::string, TicTocElement > TicTocMap
Definition
tictoc.cpp:66
g2o::cst
constexpr double cst(long double v)
Definition
misc.h:60
g2o::get_monotonic_time
double get_monotonic_time()
Definition
timeutil.cpp:43
g2o::tictoc
double tictoc(const char *algorithmPart)
Profile the timing of certain parts of your algorithm.
Definition
tictoc.cpp:118
std
Definition
jet.h:876
g2o::TicTocElement
Internal structure of the tictoc profiling.
Definition
tictoc.cpp:44
g2o::TicTocElement::clockIsRunning
bool clockIsRunning
Definition
tictoc.cpp:53
g2o::TicTocElement::exponentialMovingAverage
double exponentialMovingAverage
Definition
tictoc.cpp:50
g2o::TicTocElement::minTime
double minTime
Definition
tictoc.cpp:48
g2o::TicTocElement::numCalls
int numCalls
the number of calls
Definition
tictoc.cpp:47
g2o::TicTocElement::algorithmPart
std::string algorithmPart
name / description of the code block
Definition
tictoc.cpp:52
g2o::TicTocElement::TicTocElement
TicTocElement()
Definition
tictoc.cpp:54
g2o::TicTocElement::totalTime
double totalTime
the total time of this part of the algorithm
Definition
tictoc.cpp:46
g2o::TicTocElement::operator<
bool operator<(const TicTocElement &other) const
Definition
tictoc.cpp:62
g2o::TicTocElement::ticTime
double ticTime
the time of the last tic
Definition
tictoc.cpp:45
g2o::TicTocElement::maxTime
double maxTime
Definition
tictoc.cpp:49
g2o::TicTocInitializer
helper for printing the struct at the end of the lifetime of the program
Definition
tictoc.cpp:72
g2o::TicTocInitializer::tictocElements
TicTocMap tictocElements
Definition
tictoc.cpp:73
g2o::TicTocInitializer::enabled
bool enabled
Definition
tictoc.cpp:74
g2o::TicTocInitializer::~TicTocInitializer
~TicTocInitializer()
Definition
tictoc.cpp:76
g2o::TicTocInitializer::TicTocInitializer
TicTocInitializer()
Definition
tictoc.cpp:75
tictoc.h
timeutil.h
utility functions for handling time related stuff
Generated on
for g2o by
1.17.0