00001 /***************************************************************************** 00002 * Copyright (C) 2011 by Benjamin Hadorn (b_hadorn@bluewin.ch) 00003 ***************************************************************************** 00004 * Project : Zeus Base Library 00005 * Module : StopWatch 00006 * Package : Zeus.ZeusBase.System 00007 * Author : Benjamin Hadorn 00008 * Date : 27.12.2011 00009 * System : Zeus-Framework 00010 ***************************************************************************** 00011 * Licence: * 00012 * This library is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU Lesser General Public License as * 00014 * published by the Free Software Foundation; either version * 00015 * 2.1 of the License, or (at your option) any later version. * 00016 * * 00017 * This library is distributed in the hope that it will be useful, * 00018 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00020 * GNU Lesser General Public License for more details. * 00021 * * 00022 * You should have received a copy of the GNU Lesser General Public * 00023 * License along with this library; if not, write to the Free Software * 00024 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA * 00025 *****************************************************************************/ 00026 00027 /***************************************************************************** 00028 * Changes: 00029 * 27.12.2011 bha: created zeus 2.0 00030 *****************************************************************************/ 00031 00032 #ifndef StopWatchHPP 00033 #define StopWatchHPP 00034 00035 #include <zeusbase/System/Time.h> 00036 #include <stdio.h> 00037 00038 BEGIN_NAMESPACE_Zeus 00039 00040 /*****************************************************************************/ 00043 /*****************************************************************************/ 00044 class TStopWatch 00045 { 00046 public: 00047 /*************************************************************************/ 00050 /*************************************************************************/ 00051 inline TStopWatch(bool bStart = true) 00052 { 00053 m_fStartTime = 0; 00054 m_fStopTime = 0; 00055 m_fPausedTime = 0; 00056 if (bStart) 00057 { 00058 start(); 00059 } 00060 } 00061 00062 /*************************************************************************/ 00065 /*************************************************************************/ 00066 inline void start() 00067 { 00068 m_fStartTime = TTime::getTimeStamp(); 00069 } 00070 00071 /*************************************************************************/ 00074 /*************************************************************************/ 00075 inline Float stop() 00076 { 00077 m_fStopTime = TTime::getTimeStamp(); 00078 return getStoppedTime(); 00079 } 00080 00081 /*************************************************************************/ 00084 /*************************************************************************/ 00085 inline void stopAndPrint(const TString& rEvent) 00086 { 00087 stop(); 00088 print(rEvent); 00089 } 00090 00091 /*************************************************************************/ 00097 /*************************************************************************/ 00098 inline bool exceedTime(const Float& rfMaxTime) 00099 { 00100 return (getTime() > rfMaxTime); 00101 } 00102 00103 /*************************************************************************/ 00106 /*************************************************************************/ 00107 inline Float getTime() const 00108 { 00109 return m_fPausedTime + (TTime::getTimeStamp() - m_fStartTime); 00110 } 00111 00112 /*************************************************************************/ 00115 /*************************************************************************/ 00116 inline Float getStoppedTime() const 00117 { 00118 return m_fPausedTime + (m_fStopTime - m_fStartTime); 00119 } 00120 00121 /*************************************************************************/ 00124 /*************************************************************************/ 00125 inline void print(const TString& rEvent) const 00126 { 00127 printf("%s [%f sec]\n", rEvent.c_str(NULL), getStoppedTime()); 00128 } 00129 00130 /*************************************************************************/ 00133 /*************************************************************************/ 00134 inline void pause() 00135 { 00136 m_fPausedTime = stop(); 00137 m_fStopTime = 0; 00138 m_fStartTime = 0; 00139 } 00140 00141 /*************************************************************************/ 00144 /*************************************************************************/ 00145 inline void reset() 00146 { 00147 stop(); 00148 m_fPausedTime = 0; 00149 } 00150 00151 00152 private: 00154 Float m_fStartTime; 00156 Float m_fStopTime; 00158 Float m_fPausedTime; 00159 }; 00160 00161 00162 END_NAMESPACE_Zeus 00163 00164 #endif 00165 00166