00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 #ifndef _ABKTIMER_H_
00072 #define _ABKTIMER_H_
00073
00074 #include <time.h>
00075 #include <iostream>
00076 #include "abkassert.h"
00077
00078
00079
00080
00081
00082 #ifdef WIN32
00083 class WinTimes;
00084 #endif
00085
00086 #if defined(sun)
00087 #include <sys/resource.h>
00088 extern "C" int getrusage(int who, struct rusage *rusage);
00089 #elif defined(linux)
00090 #include <sys/resource.h>
00091 #endif
00092
00093
00094 class Timer
00095 {
00096 #ifndef WIN32
00097 time_t realTime1, realTime2;
00098 #endif
00099 double UserTime1, UserTime2;
00100 #if defined(linux) || defined(sun)
00101 double SysTime1, SysTime2;
00102 #endif
00103 double timeLimit;
00104
00105 #ifdef WIN32
00106 double getRealTimeOnTheFly();
00107 double getUserTimeOnTheFly();
00108 double getCombTimeOnTheFly();
00109 #else
00110 inline double getRealTimeOnTheFly();
00111 inline double getUserTimeOnTheFly();
00112 #if defined(sun) || defined(linux)
00113 inline double getSysTimeOnTheFly();
00114 #endif
00115 inline double getCombTimeOnTheFly();
00116
00117 #endif //WIN32
00118
00119 #if defined(sun) || defined (linux)
00120 struct rusage _ru;
00121 #endif
00122
00123 #ifdef WIN32
00124 WinTimes *_winTimes;
00125 #endif
00126
00127 protected:
00128 enum {TIMER_ON, TIMER_OFF, TIMER_SPLIT } status;
00129 public:
00130
00131 Timer(double limitInSec=0.0);
00132 #ifndef WIN32
00133 ~Timer() { };
00134 #else
00135 ~Timer();
00136 #endif
00137 void start(double limitInSec=0.0);
00138 void stop();
00139 void split();
00140 void resume();
00141 bool isStopped() const { return status == TIMER_OFF; }
00142
00143 double getUserTime() const;
00144
00145 double getSysTime() const;
00146 double getCombTime() const;
00147
00148
00149 double getRealTime() const;
00150 double getUnixTime() const;
00151
00152
00153 inline bool expired();
00154
00155
00156
00157
00158
00159
00160
00161
00162 inline bool realTimeExpired();
00163
00164 friend std::ostream& operator<<(std::ostream& os, const Timer& tm );
00165 };
00166
00167
00168 #ifndef WIN32
00169 inline double Timer::getRealTimeOnTheFly()
00170 {
00171 time(&realTime2);
00172 return difftime(realTime2,realTime1);
00173 }
00174
00175 inline double Timer::getUserTimeOnTheFly()
00176 {
00177 #if defined(sun) || defined(gnu)
00178 abkfatal(getrusage(RUSAGE_SELF,&_ru)!=-1,"Can't get time");
00179 return _ru.ru_utime.tv_sec+1e-6*_ru.ru_utime.tv_usec - UserTime1;
00180 #else
00181 return (clock()-UserTime1+0.0)/CLOCKS_PER_SEC;
00182 #endif
00183 }
00184
00185 #if defined(sun) || defined(linux)
00186 inline double Timer::getSysTimeOnTheFly()
00187 {
00188 abkfatal(getrusage(RUSAGE_SELF,&_ru)!=-1,"Can't get time");
00189 return _ru.ru_stime.tv_sec+1e-6*_ru.ru_stime.tv_usec - SysTime1;
00190 }
00191 #endif
00192
00193 inline double Timer::getCombTimeOnTheFly()
00194 {
00195 #if defined(sun) || defined(linux)
00196 abkfatal(getrusage(RUSAGE_SELF,&_ru)!=-1,"Can't get time");
00197 return _ru.ru_utime.tv_sec+1e-6*_ru.ru_utime.tv_usec - UserTime1
00198 +_ru.ru_stime.tv_sec+1e-6*_ru.ru_stime.tv_usec - SysTime1;
00199 #else
00200 return getUserTimeOnTheFly();
00201 #endif
00202 }
00203 #endif //WIN32
00204
00205 inline bool Timer::expired()
00206 {
00207 return (timeLimit<getCombTimeOnTheFly()) && (timeLimit!=0.0);
00208 }
00209
00210 inline bool Timer::realTimeExpired()
00211 {
00212 return (timeLimit<getRealTimeOnTheFly()) && (timeLimit!=0.0);
00213 }
00214
00215 #endif