Class CTimeLogger

Nested Relationships

Nested Types

Inheritance Relationships

Base Type

Class Documentation

class CTimeLogger : public mrpt::system::COutputLogger

A versatile “profiler” that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats. The results can be dumped to cout or to Visual Studio’s output panel. This class can be also used to monitorize min/mean/max/total stats of any user-provided parameters via the method CTimeLogger::registerUserMeasure().

Optional recording of all data can be enabled via enableKeepWholeHistory() (use with caution!).

Cost of the profiler itself (measured on MSVC2015, Windows 10, Intel i5-2310 2.9GHz):

  • enter(): average 445 ns

  • leave(): average 316 ns

    Recursive methods are supported with no problems, that is, calling “enter(X)

    enter(X) … leave(X) leave(X)”.

    enter()/leave() are thread-safe, in the sense of they being safe to be called from different threads. However, calling enter()/leave() for the same user-supplied “section name”, from different threads, is not allowed. In the latter case (and, actually, in general since it’s safer against exceptions), use the RAII helper class CTimeLoggerEntry.

See also

CTimeLoggerEntry

Note

The default behavior is dumping all the information at destruction.

Public Functions

CTimeLogger(bool enabled = true, const std::string &name = "", const bool keep_whole_history = false)
~CTimeLogger() override

Destructor

CTimeLogger(const CTimeLogger &o) = default
CTimeLogger &operator=(const CTimeLogger &o) = default
CTimeLogger(CTimeLogger &&o) = default
CTimeLogger &operator=(CTimeLogger &&o) = default
std::string getStatsAsText(size_t column_width = 80) const

Dump all stats to a multi-line text string.

See also

dumpAllStats, saveToCVSFile

void getStats(std::map<std::string, TCallStats> &out_stats) const

Returns all the current stats as a map: section_name => stats.

See also

getStatsAsText, dumpAllStats, saveToCVSFile

void dumpAllStats(size_t column_width = 80) const

Dump all stats through the COutputLogger interface.

See also

getStatsAsText, saveToCVSFile

void clear(bool deep_clear = false)

Resets all stats. By default (deep_clear=false), all section names are remembered (not freed) so the cost of creating upon the first next call is avoided.

Note

By design, calling this method is the only one which is not thread safe. It’s not made thread-safe to save the performance cost. Please, ensure that you call clear() only while there are no other threads registering annotations in the object.

inline void enable(bool enabled = true)
inline void disable()
inline bool isEnabled() const
inline void enableKeepWholeHistory(bool enable = true)
inline bool isEnabledKeepWholeHistory() const
void saveToCSVFile(const std::string &csv_file) const

Dump all stats to a Comma Separated Values (CSV) file.

See also

dumpAllStats

void saveToMFile(const std::string &m_file) const

Dump all stats to a Matlab/Octave (.m) file.

See also

dumpAllStats

void registerUserMeasure(const std::string_view &event_name, const double value, const bool is_time = false) noexcept
inline const std::string &getName() const noexcept
void setName(const std::string &name) noexcept
inline void enter(const std::string_view &func_name) noexcept

Start of a named section

See also

enter

inline double leave(const std::string_view &func_name) noexcept

End of a named section

See also

enter

Returns:

The ellapsed time, in seconds or 0 if disabled.

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” with that section name

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” with that section name

Protected Types

using TDataMap = mrpt::containers::ts_hash_map<std::string, TCallData, HASH_SIZE_IN_BYTES, HASH_ALLOWED_COLLISIONS>

Protected Functions

void do_enter(const std::string_view &func_name) noexcept
double do_leave(const std::string_view &func_name) noexcept

Protected Attributes

TDataMap m_data

Protected Static Attributes

static constexpr unsigned int HASH_SIZE_IN_BYTES = 1
static constexpr unsigned int HASH_ALLOWED_COLLISIONS = 10

Friends

friend struct CTimeLoggerEntry
struct TCallStats

Data of each call section: # of calls, minimum, maximum, average and overall execution time (in seconds)

See also

getStats

Public Members

size_t n_calls = {0}
double min_t = {0}
double max_t = {0}
double mean_t = {0}
double total_t = {0}
double last_t = {0}