Main MRPT website > C++ reference
MRPT logo
CTimeLogger.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CTimeLogger_H
29 #define CTimeLogger_H
30 
31 #include <mrpt/utils/CTicTac.h>
33 
34 #include <stack>
35 
36 namespace mrpt
37 {
38  namespace utils
39  {
40  using namespace std;
41 
42  /** A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
43  * The results can be dumped to cout or to Visual Studio's output panel.
44  * Recursive methods are supported with no problems, that is, calling "enter(X) enter(X) ... leave(X) leave(X)".
45  * \note The default behavior is dumping all the information at destruction.
46  * \ingroup mrpt_base_grp
47  */
49  {
50  private:
52  bool m_enabled;
53 
54  //! Data of all the calls:
55  struct TCallData
56  {
57  TCallData();
58 
59  size_t n_calls;
60  double min_t,max_t,mean_t;
61  stack<double,vector<double> > open_calls;
62  };
63 
64  map<string,TCallData> m_data;
65 
66  void do_enter( const char *func_name );
67  double do_leave( const char *func_name );
68 
69  public:
70  CTimeLogger(bool enabled = true); //! Default constructor
71  virtual ~CTimeLogger(); //!< Destructor
72  std::string getStatsAsText(const size_t column_width=80) const; //!< Dump all stats to a multi-line text string. \sa dumpAllStats, saveToCVSFile
73  void dumpAllStats(const size_t column_width=80) const; //!< Dump all stats through the CDebugOutputCapable interface. \sa getStatsAsText, saveToCVSFile
74  void clear();
75  void enable(bool enabled = true) { m_enabled = enabled; }
76  void disable() { m_enabled = false; }
77  void saveToCSVFile(const std::string &csv_file) const; //!< Dump all stats to a Comma Separated Values (CSV) file. \sa dumpAllStats
78 
79  /** Start of a named section \sa enter */
80  inline void enter( const char *func_name ) {
81  if (m_enabled)
82  do_enter(func_name);
83  }
84  /** End of a named section \return The ellapsed time, in seconds or 0 if disabled. \sa enter */
85  inline double leave( const char *func_name ) {
86  return m_enabled ? do_leave(func_name) : 0;
87  }
88  /** Return the mean execution time of the given "section", or 0 if it hasn't ever been called "enter" with that section name */
89  double getMeanTime(const std::string &name) const;
90  }; // End of class def.
91 
92 
93  /** @name Auxiliary stuff for the global profiler used in MRPT_START / MRPT_END macros.
94  @{ */
95  extern CTimeLogger BASE_IMPEXP global_profiler;
96  void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
97  void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
98  /** @} */
99 
100  } // End of namespace
101 } // End of namespace
102 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013