g2o
Loading...
Searching...
No Matches
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
39namespace g2o {
40
45 double ticTime;
46 double totalTime;
48 double minTime;
49 double maxTime;
52 std::string algorithmPart;
55 : ticTime(0.),
56 totalTime(0.),
57 numCalls(0),
58 minTime(std::numeric_limits<double>::max()),
59 maxTime(0.),
61 clockIsRunning(true) {}
62 bool operator<(const TicTocElement& other) const {
63 return totalTime < other.totalTime;
64 }
65};
66typedef std::map<std::string, TicTocElement> TicTocMap;
67
74 bool enabled;
75 TicTocInitializer() { enabled = getenv("G2O_ENABLE_TICTOC") != NULL; }
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
118double 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
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
155ScopedTictoc::ScopedTictoc(const char* algorithmPart)
156 : _algorithmPart(algorithmPart) {
157 tictoc(_algorithmPart.c_str());
158}
159
161
162} // namespace g2o
std::string _algorithmPart
Definition tictoc.h:65
ScopedTictoc(const char *algorithmPart)
Definition tictoc.cpp:155
some general case utility functions
std::map< std::string, TicTocElement > TicTocMap
Definition tictoc.cpp:66
constexpr double cst(long double v)
Definition misc.h:60
double get_monotonic_time()
Definition timeutil.cpp:43
double tictoc(const char *algorithmPart)
Profile the timing of certain parts of your algorithm.
Definition tictoc.cpp:118
Definition jet.h:876
Internal structure of the tictoc profiling.
Definition tictoc.cpp:44
double exponentialMovingAverage
Definition tictoc.cpp:50
int numCalls
the number of calls
Definition tictoc.cpp:47
std::string algorithmPart
name / description of the code block
Definition tictoc.cpp:52
double totalTime
the total time of this part of the algorithm
Definition tictoc.cpp:46
bool operator<(const TicTocElement &other) const
Definition tictoc.cpp:62
double ticTime
the time of the last tic
Definition tictoc.cpp:45
helper for printing the struct at the end of the lifetime of the program
Definition tictoc.cpp:72
TicTocMap tictocElements
Definition tictoc.cpp:73
utility functions for handling time related stuff