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://www.mrpt.org/                                |
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                  * \ingroup mrpt_base_grp
00047                  */
00048                 class BASE_IMPEXP CTimeLogger : public mrpt::utils::CDebugOutputCapable
00049                 {
00050                 private:
00051                         CTicTac         m_tictac;
00052                         bool            m_enabled;
00053 
00054                         //! Data of all the calls:
00055                         struct TCallData
00056                         {
00057                                 TCallData();
00058 
00059                                 size_t n_calls;
00060                                 double min_t,max_t,mean_t;
00061                                 stack<double,vector<double> >   open_calls;
00062                         };
00063 
00064                         map<string,TCallData>  m_data;
00065 
00066                         void do_enter( const char *func_name );
00067                         double do_leave( const char *func_name );
00068 
00069                 public:
00070                         CTimeLogger(bool enabled = true); //! Default constructor
00071                         virtual ~CTimeLogger(); //!< Destructor
00072                         std::string getStatsAsText(const size_t column_width=80) const; //!< Dump all stats to a multi-line text string. \sa dumpAllStats, saveToCVSFile
00073                         void dumpAllStats(const size_t column_width=80) const; //!< Dump all stats through the CDebugOutputCapable interface. \sa getStatsAsText, saveToCVSFile
00074                         void clear();
00075                         void enable(bool enabled = true) { m_enabled = enabled; }
00076                         void disable() { m_enabled = false; }
00077                         void saveToCSVFile(const std::string &csv_file)  const;         //!< Dump all stats to a Comma Separated Values (CSV) file. \sa dumpAllStats
00078 
00079                         /** Start of a named section \sa enter */
00080                         inline void enter( const char *func_name ) {
00081                                 if (m_enabled)
00082                                         do_enter(func_name);
00083                         }
00084                         /** End of a named section \return The ellapsed time, in seconds or 0 if disabled. \sa enter */
00085                         inline double leave( const char *func_name ) {
00086                                 return m_enabled ? do_leave(func_name) : 0;
00087                         }
00088                         /** Return the mean execution time of the given "section", or 0 if it hasn't ever been called "enter" with that section name */
00089                         double getMeanTime(const std::string &name) const;
00090                 }; // End of class def.
00091 
00092 
00093                 /** @name Auxiliary stuff for the global profiler used in MRPT_START / MRPT_END macros.
00094                   @{ */
00095                 extern CTimeLogger BASE_IMPEXP global_profiler;
00096                 void BASE_IMPEXP global_profiler_enter(const char *func_name) MRPT_NO_THROWS;
00097                 void BASE_IMPEXP global_profiler_leave(const char *func_name) MRPT_NO_THROWS;
00098                 /** @} */
00099 
00100         } // End of namespace
00101 } // End of namespace
00102 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011