Main MRPT website > C++ reference
MRPT logo

CTimeLogger.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  CTimeLogger_H
00029 #define  CTimeLogger_H
00030 
00031 #include <mrpt/utils/CTicTac.h>
00032 #include <mrpt/utils/CDebugOutputCapable.h>
00033 
00034 #include <stack>
00035 
00036 namespace mrpt
00037 {
00038         namespace utils
00039         {
00040                 using namespace std;
00041 
00042                 /** A versatile "profiler" that logs the time spent within each pair of calls to enter(X)-leave(X), among other stats.
00043                  *  The results can be dumped to cout or to Visual Studio's output panel.
00044                  *  Recursive methods are supported with no problems, that is, calling "enter(X) enter(X) ... leave(X) leave(X)".
00045                  * \note The default behavior is dumping all the information at destruction.
00046                  */
00047                 class BASE_IMPEXP CTimeLogger : public mrpt::utils::CDebugOutputCapable
00048                 {
00049                 private:
00050                         CTicTac         m_tictac;
00051                         bool            m_enabled;
00052 
00053                         //! Data of all the calls:
00054                         struct TCallData
00055                         {
00056                                 TCallData();
00057 
00058                                 size_t n_calls;
00059                                 double min_t,max_t,mean_t;
00060                                 stack<double,vector<double> >   open_calls;
00061                         };
00062 
00063                         map<string,TCallData>  m_data;
00064 
00065                         void do_enter( const char *func_name );
00066                         double do_leave( const char *func_name );
00067 
00068                 public:
00069                         CTimeLogger(bool enabled = true); //! Default constructor
00070                         virtual ~CTimeLogger(); //!< Destructor
00071                         std::string getStatsAsText(const size_t column_width=80) const; //!< Dump all stats to a multi-line text string. \sa dumpAllStats, saveToCVSFile
00072                         void dumpAllStats(const size_t column_width=80) const; //!< Dump all stats through the CDebugOutputCapable interface. \sa getStatsAsText, saveToCVSFile
00073                         void clear();
00074                         void enable(bool enabled = true) { m_enabled = enabled; }
00075                         void disable() { m_enabled = false; }
00076                         void saveToCSVFile(const std::string &csv_file)  const;         //!< Dump all stats to a Comma Separated Values (CSV) file. \sa dumpAllStats
00077 
00078                         /** Start of a named section \sa enter */
00079                         inline void enter( const char *func_name ) {
00080                                 if (m_enabled)
00081                                         do_enter(func_name);
00082                         }
00083                         /** End of a named section \return The ellapsed time, in seconds or 0 if disabled. \sa enter */
00084                         inline double leave( const char *func_name ) {
00085                                 return m_enabled ? do_leave(func_name) : 0;
00086                         }
00087                         /** Return the mean execution time of the given "section", or 0 if it hasn't ever been called "enter" with that section name */
00088                         double getMeanTime(const std::string &name) const;
00089                 }; // End of class def.
00090 
00091 
00092                 /** @name Auxiliary stuff for the global profiler used in MRPT_START / MRPT_END macros.
00093                   @{ */
00094                 extern CTimeLogger BASE_IMPEXP global_profiler;
00095                 void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
00096                 void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
00097                 /** @} */
00098 
00099         } // End of namespace
00100 } // End of namespace
00101 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011