00001
00002
00003
00004 #ifndef _CoinTime_hpp
00005 #define _CoinTime_hpp
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include <ctime>
00015 #if defined(_MSC_VER)
00016
00017 # pragma warning(disable:4786)
00018 #else
00019
00020 #if defined(__MACH__) || defined (__FreeBSD__)
00021 #include <sys/time.h>
00022 #endif
00023 #if !defined(__MSVCRT__)
00024 #include <sys/resource.h>
00025 #endif
00026 #endif
00027
00028
00029
00030 #if defined(_MSC_VER)
00031
00032 #if 0 // change this to 1 if want to use the win32 API
00033 #include <windows.h>
00034 #ifdef small
00035
00036
00037 #undef small
00038 #endif
00039 #define TWO_TO_THE_THIRTYTWO 4294967296.0
00040 #define DELTA_EPOCH_IN_SECS 11644473600.0
00041 inline double CoinGetTimeOfDay()
00042 {
00043 FILETIME ft;
00044
00045 GetSystemTimeAsFileTime(&ft);
00046 double t = ft.dwHighDateTime * TWO_TO_THE_THIRTYTWO + ft.dwLowDateTime;
00047 t = t/10000000.0 - DELTA_EPOCH_IN_SECS;
00048 return t;
00049 }
00050 #else
00051 #include <sys/types.h>
00052 #include <sys/timeb.h>
00053 inline double CoinGetTimeOfDay()
00054 {
00055 struct _timeb timebuffer;
00056 #pragma warning(disable:4996)
00057 _ftime( &timebuffer );
00058 #pragma warning(default:4996)
00059 return timebuffer.time + timebuffer.millitm/1000.0;
00060 }
00061 #endif
00062
00063 #else
00064
00065 #include <sys/time.h>
00066
00067 inline double CoinGetTimeOfDay()
00068 {
00069 struct timeval tv;
00070 gettimeofday(&tv, NULL);
00071 return tv.tv_sec + tv.tv_usec/1000000.0;
00072 }
00073
00074 #endif // _MSC_VER
00075
00084 inline double CoinWallclockTime(double callType = 0)
00085 {
00086 double callTime = CoinGetTimeOfDay();
00087 static const double firstCall = callType > 0 ? callType : callTime;
00088 return callType < 0 ? firstCall : callTime - firstCall;
00089 }
00090
00091
00092
00093 static inline double CoinCpuTime()
00094 {
00095 double cpu_temp;
00096 #if defined(_MSC_VER) || defined(__MSVCRT__)
00097 unsigned int ticksnow;
00098
00099 ticksnow = (unsigned int)clock();
00100
00101 cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
00102 #else
00103 struct rusage usage;
00104 # ifdef ZEROFAULT
00105 usage.ru_utime.tv_sec = 0 ;
00106 usage.ru_utime.tv_usec = 0 ;
00107 # endif
00108 getrusage(RUSAGE_SELF,&usage);
00109 cpu_temp = usage.ru_utime.tv_sec;
00110 cpu_temp += 1.0e-6*(static_cast<double> (usage.ru_utime.tv_usec));
00111 #endif
00112 return cpu_temp;
00113 }
00114
00115
00116
00117 static inline double CoinSysTime()
00118 {
00119 double sys_temp;
00120 #if defined(_MSC_VER) || defined(__MSVCRT__)
00121 sys_temp = 0.0;
00122 #else
00123 struct rusage usage;
00124 # ifdef ZEROFAULT
00125 usage.ru_utime.tv_sec = 0 ;
00126 usage.ru_utime.tv_usec = 0 ;
00127 # endif
00128 getrusage(RUSAGE_SELF,&usage);
00129 sys_temp = usage.ru_stime.tv_sec;
00130 sys_temp += 1.0e-6*(static_cast<double> (usage.ru_stime.tv_usec));
00131 #endif
00132 return sys_temp;
00133 }
00134
00135
00136
00137 static inline double CoinCpuTimeJustChildren()
00138 {
00139 double cpu_temp;
00140 #if defined(_MSC_VER) || defined(__MSVCRT__)
00141 cpu_temp = 0.0;
00142 #else
00143 struct rusage usage;
00144 # ifdef ZEROFAULT
00145 usage.ru_utime.tv_sec = 0 ;
00146 usage.ru_utime.tv_usec = 0 ;
00147 # endif
00148 getrusage(RUSAGE_CHILDREN,&usage);
00149 cpu_temp = usage.ru_utime.tv_sec;
00150 cpu_temp += 1.0e-6*(static_cast<double> (usage.ru_utime.tv_usec));
00151 #endif
00152 return cpu_temp;
00153 }
00154
00155
00156 #include <fstream>
00157
00173 class CoinTimer
00174 {
00175 private:
00177 double start;
00179 double limit;
00180 double end;
00181 #ifdef COIN_COMPILE_WITH_TRACING
00182 std::fstream* stream;
00183 bool write_stream;
00184 #endif
00185
00186 private:
00187 #ifdef COIN_COMPILE_WITH_TRACING
00188 inline bool evaluate(bool b_tmp) const {
00189 int i_tmp = b_tmp;
00190 if (stream) {
00191 if (write_stream)
00192 (*stream) << i_tmp << "\n";
00193 else
00194 (*stream) >> i_tmp;
00195 }
00196 return i_tmp;
00197 }
00198 inline double evaluate(double d_tmp) const {
00199 if (stream) {
00200 if (write_stream)
00201 (*stream) << d_tmp << "\n";
00202 else
00203 (*stream) >> d_tmp;
00204 }
00205 return d_tmp;
00206 }
00207 #else
00208 inline bool evaluate(const bool b_tmp) const {
00209 return b_tmp;
00210 }
00211 inline double evaluate(const double d_tmp) const {
00212 return d_tmp;
00213 }
00214 #endif
00215
00216 public:
00218 CoinTimer() :
00219 start(0), limit(1e100), end(1e100)
00220 #ifdef COIN_COMPILE_WITH_TRACING
00221 , stream(0), write_stream(true)
00222 #endif
00223 {}
00224
00226 CoinTimer(double lim) :
00227 start(CoinCpuTime()), limit(lim), end(start+lim)
00228 #ifdef COIN_COMPILE_WITH_TRACING
00229 , stream(0), write_stream(true)
00230 #endif
00231 {}
00232
00233 #ifdef COIN_COMPILE_WITH_TRACING
00234
00236 CoinTimer(std::fstream* s, bool write) :
00237 start(0), limit(1e100), end(1e100),
00238 stream(s), write_stream(write) {}
00239
00242 CoinTimer(double lim, std::fstream* s, bool w) :
00243 start(CoinCpuTime()), limit(lim), end(start+lim),
00244 stream(s), write_stream(w) {}
00245 #endif
00246
00248 inline void restart() { start=CoinCpuTime(); end=start+limit; }
00250 inline void reset() { restart(); }
00252 inline void reset(double lim) { limit=lim; restart(); }
00253
00256 inline bool isPastPercent(double pct) const {
00257 return evaluate(start + limit * pct < CoinCpuTime());
00258 }
00261 inline bool isPast(double lim) const {
00262 return evaluate(start + lim < CoinCpuTime());
00263 }
00266 inline bool isExpired() const {
00267 return evaluate(end < CoinCpuTime());
00268 }
00269
00271 inline double timeLeft() const {
00272 return evaluate(end - CoinCpuTime());
00273 }
00274
00276 inline double timeElapsed() const {
00277 return evaluate(CoinCpuTime() - start);
00278 }
00279
00280 inline void setLimit(double l) {
00281 limit = l;
00282 return;
00283 }
00284 };
00285
00286 #endif