g2o
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 "timeutil.h"
30 
31 #include <map>
32 #include <limits>
33 #include <algorithm>
34 #include <vector>
35 #include <cstdio>
36 #include <cstdlib>
37 
38 namespace g2o {
39 
44  {
45  double ticTime;
46  double totalTime;
47  int numCalls;
48  double minTime;
49  double maxTime;
51  std::string algorithmPart;
54  ticTime(0.), totalTime(0.), numCalls(0),
55  minTime(std::numeric_limits<double>::max()),
56  maxTime(0.), exponentialMovingAverage(0.),
57  clockIsRunning(true)
58  {}
59  bool operator<(const TicTocElement& other) const
60  {
61  return totalTime < other.totalTime;
62  }
63  };
64  typedef std::map<std::string, TicTocElement> TicTocMap;
65 
70  {
71  TicTocMap tictocElements;
72  bool enabled;
74  {
75  enabled = getenv("G2O_ENABLE_TICTOC") != NULL;
76  }
78  {
79  if (!enabled) {
80  return;
81  }
82 
83  if (tictocElements.size() > 0) {
84  int longestName = 0;
85  // sort the elements according to the total time and print a table
86  std::vector<TicTocElement> sortedElements;
87  sortedElements.reserve(tictocElements.size());
88  for (TicTocMap::const_iterator it = tictocElements.begin(); it != tictocElements.end(); ++it) {
89  if (it->second.numCalls == 0)
90  continue;
91  longestName = std::max(longestName, (int)it->first.size());
92  sortedElements.push_back(it->second);
93  }
94  std::sort(sortedElements.begin(), sortedElements.end());
95 
96  longestName += 4;
97 
98  // now print the table to stdout
99  printf("------------------------------------------\n");
100  printf("| TICTOC STATISTICS |\n");
101  printf("------------------------------------------\n");
102  for(std::vector<TicTocElement>::const_iterator it = sortedElements.begin(); 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("numCalls= %d\t total= %.4f\t avg= %.4f\t min= %.4f\t max= %.4f\t ema= %.4f\n",
108  it->numCalls, it->totalTime, avgTime, it->minTime, it->maxTime, it->exponentialMovingAverage);
109  }
110  printf("------------------------------------------\n");
111  }
112  }
113  };
114 
115  double tictoc(const char* algorithmPart)
116  {
117  static TicTocInitializer initializer;
118  if (! initializer.enabled)
119  return 0.;
120 
121  TicTocMap& tictocElements = initializer.tictocElements;
122  static double alpha = 0.01;
123  double now = get_monotonic_time();
124 
125  double dt = 0.;
126  TicTocMap::iterator foundIt = tictocElements.find(algorithmPart);
127  if (foundIt == tictocElements.end()) {
128  // insert element
129  TicTocElement e;
130  e.ticTime = now;
132  tictocElements[e.algorithmPart] = e;
133  } else {
134  if (foundIt->second.clockIsRunning) {
135  dt = now - foundIt->second.ticTime;
136  foundIt->second.totalTime += dt;
137  foundIt->second.minTime = std::min(foundIt->second.minTime, dt);
138  foundIt->second.maxTime = std::max(foundIt->second.maxTime, dt);
139  if (foundIt->second.numCalls == 0)
140  foundIt->second.exponentialMovingAverage = dt;
141  else
142  foundIt->second.exponentialMovingAverage = (1. - alpha) * foundIt->second.exponentialMovingAverage + alpha*dt;
143  foundIt->second.numCalls++;
144  } else {
145  foundIt->second.ticTime = now;
146  }
147  foundIt->second.clockIsRunning = !foundIt->second.clockIsRunning;
148  }
149  return dt;
150  }
151 
153  _algorithmPart(algorithmPart)
154  {
155  tictoc(_algorithmPart.c_str());
156  }
157 
159  {
160  tictoc(_algorithmPart.c_str());
161  }
162 
163 } // end namespace
double get_monotonic_time()
Definition: timeutil.cpp:113
double totalTime
the total time of this part of the algorithm
Definition: tictoc.cpp:46
std::string algorithmPart
name / description of the code block
Definition: tictoc.cpp:51
helper for printing the struct at the end of the lifetime of the program
Definition: tictoc.cpp:69
TicTocMap tictocElements
Definition: tictoc.cpp:71
std::string _algorithmPart
Definition: tictoc.h:65
utility functions for handling time related stuff
int numCalls
the number of calls
Definition: tictoc.cpp:47
double tictoc(const char *algorithmPart)
Profile the timing of certain parts of your algorithm.
Definition: tictoc.cpp:115
ScopedTictoc(const char *algorithmPart)
Definition: tictoc.cpp:152
double exponentialMovingAverage
exponential moving average with alpha = 0.01
Definition: tictoc.cpp:50
Internal structure of the tictoc profiling.
Definition: tictoc.cpp:43
bool operator<(const TicTocElement &other) const
Definition: tictoc.cpp:59
double ticTime
the time of the last tic
Definition: tictoc.cpp:45
std::map< std::string, TicTocElement > TicTocMap
Definition: tictoc.cpp:64