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 #include <fstream>
00055
00071 class CoinTimer
00072 {
00073 private:
00075 double start;
00077 double limit;
00078 double end;
00079 #ifdef COIN_COMPILE_WITH_TRACING
00080 std::fstream* stream;
00081 bool write_stream;
00082 #endif
00083
00084 private:
00085 #ifdef COIN_COMPILE_WITH_TRACING
00086 inline bool evaluate(bool b_tmp) const {
00087 int i_tmp = b_tmp;
00088 if (stream) {
00089 if (write_stream)
00090 (*stream) << i_tmp << "\n";
00091 else
00092 (*stream) >> i_tmp;
00093 }
00094 return i_tmp;
00095 }
00096 inline double evaluate(double d_tmp) const {
00097 if (stream) {
00098 if (write_stream)
00099 (*stream) << d_tmp << "\n";
00100 else
00101 (*stream) >> d_tmp;
00102 }
00103 return d_tmp;
00104 }
00105 #else
00106 inline bool evaluate(const bool b_tmp) const {
00107 return b_tmp;
00108 }
00109 inline double evaluate(const double d_tmp) const {
00110 return d_tmp;
00111 }
00112 #endif
00113
00114 public:
00116 CoinTimer() :
00117 start(0), limit(1e100), end(1e100)
00118 #ifdef COIN_COMPILE_WITH_TRACING
00119 , stream(0), write_stream(true)
00120 #endif
00121 {}
00122
00124 CoinTimer(double lim) :
00125 start(CoinCpuTime()), limit(lim), end(start+lim)
00126 #ifdef COIN_COMPILE_WITH_TRACING
00127 , stream(0), write_stream(true)
00128 #endif
00129 {}
00130
00131 #ifdef COIN_COMPILE_WITH_TRACING
00132
00134 CoinTimer(std::fstream* s, bool write) :
00135 start(0), limit(1e100), end(1e100),
00136 stream(s), write_stream(write) {}
00137
00140 CoinTimer(double lim, std::fstream* s, bool w) :
00141 start(CoinCpuTime()), limit(lim), end(start+lim),
00142 stream(s), write_stream(w) {}
00143 #endif
00144
00146 inline void restart() { start=CoinCpuTime(); end=start+limit; }
00148 inline void reset() { restart(); }
00150 inline void reset(double lim) { limit=lim; restart(); }
00151
00154 inline bool isPastPercent(double pct) const {
00155 return evaluate(start + limit * pct < CoinCpuTime());
00156 }
00159 inline bool isPast(double lim) const {
00160 return evaluate(start + lim < CoinCpuTime());
00161 }
00164 inline bool isExpired() const {
00165 return evaluate(end < CoinCpuTime());
00166 }
00167
00169 inline double timeLeft() const {
00170 return evaluate(end - CoinCpuTime());
00171 }
00172
00174 inline double timeElapsed() const {
00175 return evaluate(CoinCpuTime() - start);
00176 }
00177
00178 inline void setLimit(double l) {
00179 limit = l;
00180 return;
00181 }
00182 };
00183
00184 #endif