54 ticTime(0.), totalTime(0.), numCalls(0),
55 minTime(
std::numeric_limits<double>::max()),
56 maxTime(0.), exponentialMovingAverage(0.),
64 typedef std::map<std::string, TicTocElement>
TicTocMap;
75 enabled = getenv(
"G2O_ENABLE_TICTOC") != NULL;
83 if (tictocElements.size() > 0) {
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)
91 longestName = std::max(longestName, (
int)it->first.size());
92 sortedElements.push_back(it->second);
94 std::sort(sortedElements.begin(), sortedElements.end());
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)
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);
110 printf(
"------------------------------------------\n");
122 static double alpha = 0.01;
126 TicTocMap::iterator foundIt = tictocElements.find(algorithmPart);
127 if (foundIt == tictocElements.end()) {
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;
142 foundIt->second.exponentialMovingAverage = (1. - alpha) * foundIt->second.exponentialMovingAverage + alpha*dt;
143 foundIt->second.numCalls++;
145 foundIt->second.ticTime = now;
147 foundIt->second.clockIsRunning = !foundIt->second.clockIsRunning;
153 _algorithmPart(algorithmPart)
double get_monotonic_time()
double totalTime
the total time of this part of the algorithm
std::string algorithmPart
name / description of the code block
helper for printing the struct at the end of the lifetime of the program
std::string _algorithmPart
utility functions for handling time related stuff
int numCalls
the number of calls
double tictoc(const char *algorithmPart)
Profile the timing of certain parts of your algorithm.
ScopedTictoc(const char *algorithmPart)
double exponentialMovingAverage
exponential moving average with alpha = 0.01
Internal structure of the tictoc profiling.
bool operator<(const TicTocElement &other) const
double ticTime
the time of the last tic
std::map< std::string, TicTocElement > TicTocMap