class mrpt::system::CTimeLogger


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.

The default behavior is dumping all the information at destruction.

See also:


#include <mrpt/system/CTimeLogger.h>

class CTimeLogger: public mrpt::system::COutputLogger
    // structs

    struct TCallData;
    struct TCallStats;

    // construction

        bool enabled = true,
        const std::string& name = "",
        const bool keep_whole_history = false

    CTimeLogger(const CTimeLogger& o);
    CTimeLogger(CTimeLogger&& o);

    // methods

    CTimeLogger& operator = (const CTimeLogger& o);
    CTimeLogger& operator = (CTimeLogger&& o);
    std::string getStatsAsText(size_t column_width = 80) const;
    void getStats(std::map<std::string, TCallStats>& out_stats) const;
    void dumpAllStats(size_t column_width = 80) const;
    void clear(bool deep_clear = false);
    void saveToCSVFile(const std::string& csv_file) const;
    void saveToMFile(const std::string& m_file) const;
    void enter(const std::string_view& func_name);
    double leave(const std::string_view& func_name);
    double getMeanTime(const std::string& name) const;
    double getLastTime(const std::string& name) const;

// direct descendants

struct MyGlobalProfiler;

Inherited Members

    // structs

    struct TMsg;


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.

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.

void saveToCSVFile(const std::string& csv_file) const

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

See also:


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

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

See also:


void enter(const std::string_view& func_name)

Start of a named section.

See also:


double leave(const std::string_view& func_name)

End of a named section.


The ellapsed time, in seconds or 0 if disabled.

See also:


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.