MOOS 0.2375
/home/toby/moos-ivp/MOOS-2375-Oct0611/Essentials/MOOSUtilityLib/MOOSTimeJournal.h
Go to the documentation of this file.
00001 
00002 //
00003 //   MOOS - Mission Oriented Operating Suite 
00004 //  
00005 //   A suit of Applications and Libraries for Mobile Robotics Research 
00006 //   Copyright (C) 2001-2005 Massachusetts Institute of Technology and 
00007 //   Oxford University. 
00008 //    
00009 //   This software was written by Paul Newman at MIT 2001-2002 and Oxford 
00010 //   University 2003-2005. email: pnewman@robots.ox.ac.uk. 
00011 //      
00012 //   This file is part of a  MOOS Core Component. 
00013 //        
00014 //   This program is free software; you can redistribute it and/or 
00015 //   modify it under the terms of the GNU General Public License as 
00016 //   published by the Free Software Foundation; either version 2 of the 
00017 //   License, or (at your option) any later version. 
00018 //          
00019 //   This program 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 GNU 
00022 //   General Public License for more details. 
00023 //            
00024 //   You should have received a copy of the GNU General Public License 
00025 //   along with this program; if not, write to the Free Software 
00026 //   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 
00027 //   02111-1307, USA. 
00028 //
00030 
00032 #ifndef MOOSTimeJournalH
00033 #define MOOSTimeJournalH
00034 #include <fstream>
00035 #include <map>
00036 #include <string>
00037 
00038 //this is a utility class to do time stamping. 
00039 //
00040 //Tick("LABEL");
00041 //  DO YOUR CODE
00042 //        TICK("LABEL2")
00043 //            MORE CODE
00044 //        Tock("LABLE2")
00045 //Tock("LABEL")
00046 //Dump();
00047 //
00048 //this will produce a pretty file of timings
00049 //you can call NewLevel() to print an iteration seperator
00050 //in the dump file. This an exerpt from a dump file
00051 /*Level:4754
00052     Iterate              0.000 seconds
00053 Level:4755
00054     Iterate              0.430 seconds
00055     ConstraintApplication 0.422 seconds
00056     EKF                  0.012 seconds
00057     EKF_UPD_1            0.006 seconds
00058     EKF_OBS              0.006 seconds    
00059     ConstraintSearch     0.003 seconds
00060 Level:4756
00061     Iterate              0.041 seconds
00062 Level:4757
00063     Iterate              0.000 seconds
00064 */
00065 
00066 class CMOOSTimeJournal
00067 {
00068 public:
00069 
00070     CMOOSTimeJournal()
00071     {        
00072         m_nLevel = 0;
00073         m_nStack = 0;
00074     }
00075 
00076     ~CMOOSTimeJournal()
00077     {
00078         m_File.close();
00079     }
00080 
00081     void Open(const std::string & sFile)
00082     {
00083         m_File.open(sFile.c_str());
00084         m_nLevel = 0;
00085     }
00086 
00087     void Tick(const std::string & S)
00088     {
00089         m_T[S] = HPMOOSTime();
00090         m_nStack++;
00091     }
00092 
00093     void Tock(const std::string & S)
00094     {
00095         if(m_T.find(S)==m_T.end())
00096         {
00097             MOOSTrace("No such timer \"%s\"\n",S.c_str());
00098             return;
00099         }
00100         else
00101         {            
00102             std::string space(4*(m_nStack+1),' ');
00103             std::string R= MOOSFormat("%s%-40s %.3f seconds",space.c_str(),S.c_str(),HPMOOSTime()-m_T[S]);
00104             m_L.push_back(R);
00105 
00106             if(m_nStack>0)
00107                 m_nStack--;
00108 
00109         }
00110 
00111 
00112     }
00113 
00114     void NewLevel(int nL=-1)
00115     {
00116         m_nLevel = nL>-1 ? nL:m_nLevel+1;
00117     }
00118 
00119     void Dump()
00120     {        
00121         m_File<<"Level:"<<m_nLevel<<std::endl;
00122         std::copy(m_L.rbegin(),m_L.rend(),std::ostream_iterator<std::string>(m_File,"\n"));
00123         m_File.flush();
00124         m_L.clear();
00125         m_T.clear();
00126     }
00127 
00128 protected:    
00129     std::map<std::string, double> m_T;
00130     std::list<std::string> m_L;
00131     std::ofstream m_File;
00132     int m_nLevel;
00133     int m_nStack;
00134 
00135 };
00136 
00137 
00138 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines