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 static inline double CoinCpuTime()
00031 {
00032 double cpu_temp;
00033 #if defined(_MSC_VER) || defined(__MSVCRT__)
00034 unsigned int ticksnow;
00035
00036 ticksnow = (unsigned int)clock();
00037
00038 cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
00039 #else
00040 struct rusage usage;
00041 # ifdef ZEROFAULT
00042 usage.ru_utime.tv_sec = 0 ;
00043 usage.ru_utime.tv_usec = 0 ;
00044 # endif
00045 getrusage(RUSAGE_SELF,&usage);
00046 cpu_temp = usage.ru_utime.tv_sec;
00047 cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
00048 #endif
00049 return cpu_temp;
00050 }
00051
00052
00053
00054 static inline double CoinCpuTimeJustChildren()
00055 {
00056 double cpu_temp;
00057 #if defined(_MSC_VER) || defined(__MSVCRT__)
00058 cpu_temp = 0.0;
00059 #else
00060 struct rusage usage;
00061 # ifdef ZEROFAULT
00062 usage.ru_utime.tv_sec = 0 ;
00063 usage.ru_utime.tv_usec = 0 ;
00064 # endif
00065 getrusage(RUSAGE_CHILDREN,&usage);
00066 cpu_temp = usage.ru_utime.tv_sec;
00067 cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
00068 #endif
00069 return cpu_temp;
00070 }
00071
00072
00073 #include <fstream>
00074
00090 class CoinTimer
00091 {
00092 private:
00094 double start;
00096 double limit;
00097 double end;
00098 #ifdef COIN_COMPILE_WITH_TRACING
00099 std::fstream* stream;
00100 bool write_stream;
00101 #endif
00102
00103 private:
00104 #ifdef COIN_COMPILE_WITH_TRACING
00105 inline bool evaluate(bool b_tmp) const {
00106 int i_tmp = b_tmp;
00107 if (stream) {
00108 if (write_stream)
00109 (*stream) << i_tmp << "\n";
00110 else
00111 (*stream) >> i_tmp;
00112 }
00113 return i_tmp;
00114 }
00115 inline double evaluate(double d_tmp) const {
00116 if (stream) {
00117 if (write_stream)
00118 (*stream) << d_tmp << "\n";
00119 else
00120 (*stream) >> d_tmp;
00121 }
00122 return d_tmp;
00123 }
00124 #else
00125 inline bool evaluate(const bool b_tmp) const {
00126 return b_tmp;
00127 }
00128 inline double evaluate(const double d_tmp) const {
00129 return d_tmp;
00130 }
00131 #endif
00132
00133 public:
00135 CoinTimer() :
00136 start(0), limit(1e100), end(1e100)
00137 #ifdef COIN_COMPILE_WITH_TRACING
00138 , stream(0), write_stream(true)
00139 #endif
00140 {}
00141
00143 CoinTimer(double lim) :
00144 start(CoinCpuTime()), limit(lim), end(start+lim)
00145 #ifdef COIN_COMPILE_WITH_TRACING
00146 , stream(0), write_stream(true)
00147 #endif
00148 {}
00149
00150 #ifdef COIN_COMPILE_WITH_TRACING
00151
00153 CoinTimer(std::fstream* s, bool write) :
00154 start(0), limit(1e100), end(1e100),
00155 stream(s), write_stream(write) {}
00156
00159 CoinTimer(double lim, std::fstream* s, bool w) :
00160 start(CoinCpuTime()), limit(lim), end(start+lim),
00161 stream(s), write_stream(w) {}
00162 #endif
00163
00165 inline void restart() { start=CoinCpuTime(); end=start+limit; }
00167 inline void reset() { restart(); }
00169 inline void reset(double lim) { limit=lim; restart(); }
00170
00173 inline bool isPastPercent(double pct) const {
00174 return evaluate(start + limit * pct < CoinCpuTime());
00175 }
00178 inline bool isPast(double lim) const {
00179 return evaluate(start + lim < CoinCpuTime());
00180 }
00183 inline bool isExpired() const {
00184 return evaluate(end < CoinCpuTime());
00185 }
00186
00188 inline double timeLeft() const {
00189 return evaluate(end - CoinCpuTime());
00190 }
00191
00193 inline double timeElapsed() const {
00194 return evaluate(CoinCpuTime() - start);
00195 }
00196
00197 inline void setLimit(double l) {
00198 limit = l;
00199 return;
00200 }
00201 };
00202
00203 #endif