00001 /* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ 00002 00037 #ifndef __TIMER_H 00038 #define __TIMER_H 00039 00040 #include <iostream> 00041 #include <string> 00042 //#include "MtcpCheckpointManager.h" 00043 00045 class BaseTimer { 00046 public: 00047 enum { 00048 MSPSEC = 1000000 00049 }; 00050 00051 // todo: welche Konsequenzen hat es, _start_t mit 0 zu initialisieren? 00052 inline BaseTimer():_start_t(0) { clear(); }; 00053 00055 inline void clear() { _t = 0; } 00056 00058 inline double time() const { return _t; } 00059 00061 static long seed(); 00062 00064 std::ostream &print (std::ostream &) const; 00065 00068 // -- Some arithmetic operators to compute cumulative time : 00069 BaseTimer& operator = (const BaseTimer & T) ; 00070 const BaseTimer operator - (const BaseTimer & T) const; 00071 const BaseTimer operator - () ; 00072 const BaseTimer operator + (const BaseTimer & T) const; 00073 BaseTimer& operator += (const BaseTimer & T) { return *this = *this + T; }; 00074 BaseTimer& operator -= (const BaseTimer & T) { return *this = *this - T; }; 00077 public: 00078 double _start_t; 00079 double _t; 00080 00081 }; 00082 00083 inline std::ostream &operator << (std::ostream &o, const BaseTimer &BT) 00084 { return BT.print(o); } 00085 00086 class RealTimer : public BaseTimer { 00087 public: 00088 inline RealTimer (const BaseTimer &BT) : BaseTimer (BT) { }; 00089 inline RealTimer () { }; 00090 void start (); 00091 void stop (); 00092 }; 00093 00094 00095 class UserTimer : public BaseTimer { 00096 public: 00097 inline UserTimer (const BaseTimer &BT) : BaseTimer (BT) {}; 00098 inline UserTimer () { }; 00099 void start (); 00100 void stop (); 00101 }; 00102 00103 00104 class SysTimer : public BaseTimer { 00105 public: 00106 inline SysTimer (const BaseTimer &BT): BaseTimer (BT) {}; 00107 inline SysTimer () { }; 00108 void start (); 00109 void stop (); 00110 }; 00111 00113 class Timer { 00114 00115 friend class MtcpCheckpointManager; 00116 public : 00117 00118 inline Timer () { clear(); }; 00119 00120 00122 void clear(); 00123 00125 void start (); 00126 00128 void stop (); 00129 00131 void pauseTimer (); 00132 00134 void continueTimer (); 00135 00137 double usertime () const { return ut.time(); } 00138 00140 double systime () const { return st.time(); } 00141 00143 double realtime () const { return rt.time(); } 00144 00145 // retourne une petite graine 00146 // long seed() const { return RealTimer::seed(); } 00149 // Some arithmetic operators to compute cumulative time : 00150 Timer& operator = (const Timer & T) ; 00151 const Timer operator - (const Timer & T) const; 00152 const Timer operator - () ; 00153 const Timer operator + (const Timer & T) const; 00154 Timer& operator += (const Timer & T) { return *this = *this + T; }; 00155 Timer& operator -= (const Timer & T) { return *this = *this - T; }; 00157 // -- methods : 00158 std::ostream &print (std::ostream &) const; 00159 00160 size_t count() const {return _count;} 00161 00162 private: 00163 size_t _count; // how many 00164 00165 RealTimer rt; 00166 UserTimer ut; 00167 SysTimer st; 00168 00171 bool b_IsRunning; 00172 bool b_Paused; 00174 }; 00175 00176 // inline std::ostream &operator << (std::ostream &o, const Timer &T) 00177 // { return T.print (o); } 00178 00179 inline std::ostream &operator << (std::ostream &o, const Timer &T) 00180 { 00181 double ut = T.usertime(); 00182 if (ut < 0.0000000001) ut = 0; 00183 return o << T.realtime() << "s (" << ut << " cpu) [" << T.count() << "]"; } 00184 00185 00186 00187 00188 inline void outputTimerInfoEx(std::ostream & os, Timer &tim, std::string ostring) 00189 { 00190 os << ostring << " : " << tim.usertime() << " sec." << std::endl; 00191 } 00192 00193 00194 00195 00196 //#include "timer.C" 00197 00198 #endif