Main MRPT website > C++ reference for MRPT 1.4.0
CTimeLogger.h
Go to the documentation of this file.
1/* +---------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | http://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2016, Individual contributors, see AUTHORS file |
6 | See: http://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See details in http://www.mrpt.org/License |
8 +---------------------------------------------------------------------------+ */
9#ifndef CTimeLogger_H
10#define CTimeLogger_H
11
12#include <mrpt/utils/CTicTac.h>
16#include <vector>
17#include <stack>
18#include <map>
19
20namespace mrpt
21{
22 namespace utils
23 {
24 /** A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
25 * The results can be dumped to cout or to Visual Studio's output panel.
26 * Recursive methods are supported with no problems, that is, calling "enter(X) enter(X) ... leave(X) leave(X)".
27 *
28 * This class can be also used to monitorize min/mean/max/total stats of any user-provided parameters via the method CTimeLogger::registerUserMeasure()
29 *
30 * \sa CTimeLoggerEntry
31 *
32 * \note The default behavior is dumping all the information at destruction.
33 * \ingroup mrpt_base_grp
34 */
36 {
37 private:
40
41 //! Data of all the calls:
43 {
45
46 size_t n_calls;
47 double min_t,max_t,mean_t;
48 std::stack<double,std::vector<double> > open_calls;
50 };
51
52 std::map<std::string,TCallData> m_data;
53
54 void do_enter( const char *func_name );
55 double do_leave( const char *func_name );
56
57 public:
58 /** Data of each call section: # of calls, minimum, maximum, average and overall execution time (in seconds) \sa getStats */
60 {
61 size_t n_calls;
62 double min_t,max_t,mean_t,total_t;
63 };
64
65 CTimeLogger(bool enabled = true); //! Default constructor
66 virtual ~CTimeLogger(); //!< Destructor
67 std::string getStatsAsText(const size_t column_width=80) const; //!< Dump all stats to a multi-line text string. \sa dumpAllStats, saveToCVSFile
68 void getStats(std::map<std::string,TCallStats> &out_stats) const; //!< Returns all the current stats as a map: section_name => stats. \sa getStatsAsText, dumpAllStats, saveToCVSFile
69 void dumpAllStats(const size_t column_width=80) const; //!< Dump all stats through the CDebugOutputCapable interface. \sa getStatsAsText, saveToCVSFile
70 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.
71 void enable(bool enabled = true) { m_enabled = enabled; }
72 void disable() { m_enabled = false; }
73 void saveToCSVFile(const std::string &csv_file) const; //!< Dump all stats to a Comma Separated Values (CSV) file. \sa dumpAllStats
74 void registerUserMeasure(const char *event_name, const double value);
75
76 /** Start of a named section \sa enter */
77 inline void enter( const char *func_name ) {
78 if (m_enabled)
79 do_enter(func_name);
80 }
81 /** End of a named section \return The ellapsed time, in seconds or 0 if disabled. \sa enter */
82 inline double leave( const char *func_name ) {
83 return m_enabled ? do_leave(func_name) : 0;
84 }
85 /** Return the mean execution time of the given "section", or 0 if it hasn't ever been called "enter" with that section name */
86 double getMeanTime(const std::string &name) const;
87 }; // End of class def.
88
89
90 /** A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon construction and destruction of
91 * this auxiliary object, making sure that leave() will be called upon exceptions, etc.
92 * Usage:
93 * \code
94 * CTimeLogger logger;
95 * // ...
96 * { // Start of scope to be monitorized
97 * CTimeLoggerEntry tle(logger,"operation-name");
98 *
99 * // do whatever
100 *
101 * } // End of scope
102 * \endcode
103 * \ingroup mrpt_base_grp
104 */
106 {
107 CTimeLoggerEntry(CTimeLogger &logger, const char*section_name );
110 const char *m_section_name;
111 };
112
113
114 /** @name Auxiliary stuff for the global profiler used in MRPT_START / MRPT_END macros.
115 @{ */
117 void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
118 void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
119 /** @} */
120
121 } // End of namespace
122} // End of namespace
123#endif
This base class provides a common printf-like method to send debug information to std::cout,...
This class implements a high-performance stopwatch.
Definition: CTicTac.h:25
A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X),...
Definition: CTimeLogger.h:36
void dumpAllStats(const size_t column_width=80) const
Dump all stats through the CDebugOutputCapable interface.
double do_leave(const char *func_name)
void registerUserMeasure(const char *event_name, const double value)
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...
void getStats(std::map< std::string, TCallStats > &out_stats) const
Returns all the current stats as a map: section_name => stats.
void clear(bool deep_clear=false)
Resets all stats. By default (deep_clear=false), all section names are remembered (not freed) so the ...
void enter(const char *func_name)
Start of a named section.
Definition: CTimeLogger.h:77
std::map< std::string, TCallData > m_data
Definition: CTimeLogger.h:52
void enable(bool enabled=true)
Definition: CTimeLogger.h:71
CTimeLogger(bool enabled=true)
virtual ~CTimeLogger()
Default constructor.
double leave(const char *func_name)
End of a named section.
Definition: CTimeLogger.h:82
std::string getStatsAsText(const size_t column_width=80) const
Dump all stats to a multi-line text string.
void do_enter(const char *func_name)
void saveToCSVFile(const std::string &csv_file) const
Dump all stats to a Comma Separated Values (CSV) file.
#define MRPT_NO_THROWS
Used after member declarations.
Definition: mrpt_macros.h:391
void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS
void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS
CTimeLogger BASE_IMPEXP global_profiler
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Data of all the calls:
Definition: CTimeLogger.h:43
std::stack< double, std::vector< double > > open_calls
Definition: CTimeLogger.h:48
Data of each call section: # of calls, minimum, maximum, average and overall execution time (in secon...
Definition: CTimeLogger.h:60
A safe way to call enter() and leave() of a mrpt::utils::CTimeLogger upon construction and destructio...
Definition: CTimeLogger.h:106
CTimeLogger & m_logger
Definition: CTimeLogger.h:109
~CTimeLoggerEntry()
const char * m_section_name
Definition: CTimeLogger.h:110
CTimeLoggerEntry(CTimeLogger &logger, const char *section_name)



Page generated by Doxygen 1.9.6 for MRPT 1.4.0 SVN: at Wed Mar 22 20:12:58 UTC 2023