Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

CoinTime.hpp

00001 // Copyright (C) 2002, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 
00004 #ifndef _CoinTime_hpp
00005 #define _CoinTime_hpp
00006 
00007 //#############################################################################
00008 
00009 #include <ctime>
00010 #if defined(_MSC_VER)
00011 // Turn off compiler warning about long names
00012 #  pragma warning(disable:4786)
00013 #else
00014 // MacOS-X and FreeBSD needs sys/time.h
00015 #if defined(__MACH__) || defined (__FreeBSD__)
00016 #include <sys/time.h>
00017 #endif
00018 #include <sys/resource.h>
00019 #endif
00020 
00021 //#############################################################################
00022 
00023 static inline double CoinCpuTime()
00024 {
00025   double cpu_temp;
00026 #if defined(_MSC_VER)
00027   unsigned int ticksnow;        /* clock_t is same as int */
00028   
00029   ticksnow = (unsigned int)clock();
00030   
00031   cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
00032 #else
00033   struct rusage usage;
00034   getrusage(RUSAGE_SELF,&usage);
00035   cpu_temp = usage.ru_utime.tv_sec;
00036   cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
00037 #endif
00038   return cpu_temp;
00039 }
00040 
00041 //#############################################################################
00042 
00043 #include <fstream>
00044 
00060 class CoinTimer
00061 {
00062 private:
00064    double start;
00066    double limit;
00067    double end;
00068 #ifdef COIN_COMPILE_WITH_TRACING
00069    std::fstream* stream;
00070    bool write_stream;
00071 #endif
00072 
00073 private:
00074 #ifdef COIN_COMPILE_WITH_TRACING
00075    inline bool evaluate(bool b_tmp) const {
00076       int i_tmp = b_tmp;
00077       if (stream) {
00078          if (write_stream)
00079             (*stream) << i_tmp << "\n";
00080          else 
00081             (*stream) >> i_tmp;
00082       }
00083       return i_tmp;
00084    }
00085    inline double evaluate(double d_tmp) const {
00086       if (stream) {
00087          if (write_stream)
00088             (*stream) << d_tmp << "\n";
00089          else 
00090             (*stream) >> d_tmp;
00091       }
00092       return d_tmp;
00093    }
00094 #else
00095    inline bool evaluate(const bool b_tmp) const {
00096       return b_tmp;
00097    }
00098    inline double evaluate(const double d_tmp) const {
00099       return d_tmp;
00100    }
00101 #endif   
00102 
00103 public:
00105    CoinTimer() :
00106       start(0), limit(1e100), end(1e100)
00107 #ifdef COIN_COMPILE_WITH_TRACING
00108       , stream(0), write_stream(true)
00109 #endif
00110    {}
00111 
00113    CoinTimer(double lim) :
00114       start(CoinCpuTime()), limit(lim), end(start+lim)
00115 #ifdef COIN_COMPILE_WITH_TRACING
00116       , stream(0), write_stream(true)
00117 #endif
00118    {}
00119 
00120 #ifdef COIN_COMPILE_WITH_TRACING
00121 
00123    CoinTimer(std::fstream* s, bool write) :
00124       start(0), limit(1e100), end(1e100),
00125       stream(s), write_stream(write) {}
00126    
00129    CoinTimer(double lim, std::fstream* s, bool w) :
00130       start(CoinCpuTime()), limit(lim), end(start+lim),
00131       stream(s), write_stream(w) {}
00132 #endif
00133    
00135    inline void restart() { start=CoinCpuTime(); end=start+limit; }
00137    inline void reset() { restart(); }
00139    inline void reset(double lim) { limit=lim; restart(); }
00140 
00143    inline bool isPastPercent(double pct) const {
00144       return evaluate(start + limit * pct > CoinCpuTime());
00145    }
00148    inline bool isPast(double lim) const {
00149       return evaluate(start + lim > CoinCpuTime());
00150    }
00153    inline bool isExpired() const {
00154       return evaluate(end < CoinCpuTime());
00155    }
00156 
00158    inline double timeLeft() const {
00159       return evaluate(end - CoinCpuTime());
00160    }
00161 };
00162 
00163 #endif

Generated on Wed Dec 3 14:34:24 2003 for Coin by doxygen 1.3.5