30 this->saveToCSVFile(sFil);
31 std::cout <<
"[MRPT global profiler] Write stats to: " << sFil
67 m_enabled(o.m_enabled),
74 COutputLogger::operator=(o);
82 m_enabled(o.m_enabled),
89 COutputLogger::operator=(o);
110 for (
size_t p = 0;
p <
s.size();
p +=
len)
113 if (
p +
len <
s.size()) ret +=
"\n";
121 for (
const auto e :
m_data)
124 cs.
min_t = e.second.min_t;
125 cs.
max_t = e.second.max_t;
127 cs.
mean_t = e.second.n_calls ? e.second.mean_t / e.second.n_calls : 0;
129 cs.
last_t = e.second.last_t;
137 std::string mrpt_string =
"MRPT CTimeLogger report ";
142 int space_to_fill = top_header.size() < column_width
143 ? (column_width - top_header.size()) / 2
146 top_header = dashes_half + top_header + dashes_half;
147 if (dashes_half.size() % 2)
154 " FUNCTION #CALLS MIN.T MEAN.T " 158 stats_text += top_header +
"\n";
159 stats_text += middle_header +
"\n";
160 stats_text += bottom_header +
"\n";
163 for (
const auto i :
m_data)
165 const string sMinT =
unitsFormat(i.second.min_t, 1,
false);
166 const string sMaxT =
unitsFormat(i.second.max_t, 1,
false);
167 const string sTotalT =
unitsFormat(i.second.mean_t, 1,
false);
169 i.second.n_calls ? i.second.mean_t / i.second.n_calls : 0, 1,
173 "%s %7u %6s%c %6s%c %6s%c %6s%c\n",
175 static_cast<unsigned int>(i.second.n_calls), sMinT.c_str(),
176 i.second.has_time_units ?
's' :
' ', sMeanT.c_str(),
177 i.second.has_time_units ?
's' :
' ', sMaxT.c_str(),
178 i.second.has_time_units ?
's' :
' ', sTotalT.c_str(),
179 i.second.has_time_units ?
's' :
' ');
183 stats_text += footer +
"\n";
191 s +=
"FUNCTION, #CALLS, LAST.T, MIN.T, MEAN.T, MAX.T, TOTAL.T\n";
192 for (
const auto& i :
m_data)
195 "\"%s\",\"%7u\",\"%e\",\"%e\",\"%e\",\"%e\",\"%e\"\n",
196 i.first.c_str(),
static_cast<unsigned int>(i.second.n_calls),
197 i.second.last_t, i.second.min_t,
198 i.second.n_calls ? i.second.mean_t / i.second.n_calls : 0,
199 i.second.max_t, i.second.mean_t);
201 std::ofstream(csv_file) <<
s;
211 const string s = func_name;
223 const string s = func_name;
250 const char* event_name,
const double value)
253 const string s = event_name;
272 : n_calls(0), min_t(0), max_t(0), mean_t(0), last_t(0), has_time_units(true)
282 return it->second.n_calls ? it->second.mean_t / it->second.n_calls : 0;
290 return it->second.last_t;
294 const CTimeLogger& logger,
const char* section_name)
295 : m_logger(const_cast<
CTimeLogger&>(logger)), m_section_name(section_name)
double Tac() noexcept
Stops the stopwatch.
void clear(bool deep_clear=false)
Resets all stats.
void do_enter(const char *func_name)
double do_leave(const char *func_name)
void dumpAllStats(const size_t column_width=80) const
Dump all stats through the COutputLogger interface.
mrpt::system::CTimeLogger & global_profiler_getref() noexcept
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
double getLastTime(const std::string &name) const
Return the last execution time of the given "section", or 0 if it hasn't ever been called "enter" wit...
void getStats(std::map< std::string, TCallStats > &out_stats) const
Returns all the current stats as a map: section_name => stats.
std::string getStatsAsText(const size_t column_width=80) const
Dump all stats to a multi-line text string.
MyGlobalProfiler global_profiler
std::stack< double, std::vector< double > > open_calls
void global_profiler_enter(const char *func_name) noexcept
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
Versatile class for consistent logging and management of output messages.
virtual ~CTimeLogger()
Default constructor.
CTimeLoggerEntry(const CTimeLogger &logger, const char *section_name)
double leave(const char *func_name)
End of a named section.
GLsizei const GLchar ** string
double getMeanTime(const std::string &name) const
Return the mean execution time of the given "section", or 0 if it hasn't ever been called "enter" wit...
#define MRPT_LOG_INFO_STREAM(__CONTENTS)
CTimeLogger & operator=(const CTimeLogger &o)
void global_profiler_leave(const char *func_name) noexcept
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
std::string unitsFormat(const double val, int nDecimalDigits=2, bool middle_space=true)
This function implements formatting with the appropriate SI metric unit prefix: 1e-12->'p', 1e-9->'n', 1e-6->'u', 1e-3->'m', 1->'', 1e3->'K', 1e6->'M', 1e9->'G', 1e12->'T'.
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
void saveToCSVFile(const std::string &csv_file) const
Dump all stats to a Comma Separated Values (CSV) file.
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Data of each call section: # of calls, minimum, maximum, average and overall execution time (in secon...
std::string aux_format_string_multilines(const std::string &s, const size_t len)
GLuint const GLchar * name
std::string rightPad(const std::string &str, const size_t total_len, bool truncate_if_larger=false)
Enlarge the string with spaces up to the given length.
GLsizei const GLfloat * value
void Tic() noexcept
Starts the stopwatch.
const Scalar * const_iterator
const char * m_section_name
CTimeLogger(bool enabled=true, const std::string &name="")
void registerUserMeasure(const char *event_name, const double value)
void enter(const char *func_name)
Start of a named section.