/home/coin/SVN-release/DyLP-1.5.0/CoinUtils/src/CoinTime.hpp

Go to the documentation of this file.
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 // Uncomment the next three lines for thorough memory initialisation.
00008 // #ifndef ZEROFAULT
00009 // # define ZEROFAULT
00010 // #endif
00011 
00012 //#############################################################################
00013 
00014 #include <ctime>
00015 #if defined(_MSC_VER)
00016 // Turn off compiler warning about long names
00017 #  pragma warning(disable:4786)
00018 #else
00019 // MacOS-X and FreeBSD needs sys/time.h
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 /* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
00036    '#define small char' */
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 ); // C4996
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 //#define HAVE_SDK // if SDK under Win32 is installed, for CPU instead of elapsed time under Win 
00094 #ifdef HAVE_SDK
00095 #include <windows.h>
00096 #ifdef small
00097 /* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
00098    '#define small char' */
00099 #undef small
00100 #endif
00101 #define TWO_TO_THE_THIRTYTWO 4294967296.0
00102 #endif
00103 
00104 static inline double CoinCpuTime()
00105 {
00106   double cpu_temp;
00107 #if defined(_MSC_VER) || defined(__MSVCRT__)
00108 #ifdef HAVE_SDK
00109   FILETIME creation;
00110   FILETIME exit;
00111   FILETIME kernel;
00112   FILETIME user;
00113   GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user);
00114   double t = user.dwHighDateTime * TWO_TO_THE_THIRTYTWO + user.dwLowDateTime;
00115   return t/10000000.0;
00116 #else
00117   unsigned int ticksnow;        /* clock_t is same as int */
00118   ticksnow = (unsigned int)clock();
00119   cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
00120 #endif
00121 
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   cpu_temp = usage.ru_utime.tv_sec;
00130   cpu_temp += 1.0e-6*(static_cast<double> (usage.ru_utime.tv_usec));
00131 #endif
00132   return cpu_temp;
00133 }
00134 
00135 //#############################################################################
00136 
00137 
00138 
00139 static inline double CoinSysTime()
00140 {
00141   double sys_temp;
00142 #if defined(_MSC_VER) || defined(__MSVCRT__)
00143   sys_temp = 0.0;
00144 #else
00145   struct rusage usage;
00146 # ifdef ZEROFAULT
00147   usage.ru_utime.tv_sec = 0 ;
00148   usage.ru_utime.tv_usec = 0 ;
00149 # endif
00150   getrusage(RUSAGE_SELF,&usage);
00151   sys_temp = usage.ru_stime.tv_sec;
00152   sys_temp += 1.0e-6*(static_cast<double> (usage.ru_stime.tv_usec));
00153 #endif
00154   return sys_temp;
00155 }
00156 
00157 //#############################################################################
00158 // On most systems SELF seems to include children threads, This is for when it doesn't
00159 static inline double CoinCpuTimeJustChildren()
00160 {
00161   double cpu_temp;
00162 #if defined(_MSC_VER) || defined(__MSVCRT__)
00163   cpu_temp = 0.0;
00164 #else
00165   struct rusage usage;
00166 # ifdef ZEROFAULT
00167   usage.ru_utime.tv_sec = 0 ;
00168   usage.ru_utime.tv_usec = 0 ;
00169 # endif
00170   getrusage(RUSAGE_CHILDREN,&usage);
00171   cpu_temp = usage.ru_utime.tv_sec;
00172   cpu_temp += 1.0e-6*(static_cast<double> (usage.ru_utime.tv_usec));
00173 #endif
00174   return cpu_temp;
00175 }
00176 //#############################################################################
00177 
00178 #include <fstream>
00179 
00195 class CoinTimer
00196 {
00197 private:
00199    double start;
00201    double limit;
00202    double end;
00203 #ifdef COIN_COMPILE_WITH_TRACING
00204    std::fstream* stream;
00205    bool write_stream;
00206 #endif
00207 
00208 private:
00209 #ifdef COIN_COMPILE_WITH_TRACING
00210    inline bool evaluate(bool b_tmp) const {
00211       int i_tmp = b_tmp;
00212       if (stream) {
00213          if (write_stream)
00214             (*stream) << i_tmp << "\n";
00215          else 
00216             (*stream) >> i_tmp;
00217       }
00218       return i_tmp;
00219    }
00220    inline double evaluate(double d_tmp) const {
00221       if (stream) {
00222          if (write_stream)
00223             (*stream) << d_tmp << "\n";
00224          else 
00225             (*stream) >> d_tmp;
00226       }
00227       return d_tmp;
00228    }
00229 #else
00230    inline bool evaluate(const bool b_tmp) const {
00231       return b_tmp;
00232    }
00233    inline double evaluate(const double d_tmp) const {
00234       return d_tmp;
00235    }
00236 #endif   
00237 
00238 public:
00240    CoinTimer() :
00241       start(0), limit(1e100), end(1e100)
00242 #ifdef COIN_COMPILE_WITH_TRACING
00243       , stream(0), write_stream(true)
00244 #endif
00245    {}
00246 
00248    CoinTimer(double lim) :
00249       start(CoinCpuTime()), limit(lim), end(start+lim)
00250 #ifdef COIN_COMPILE_WITH_TRACING
00251       , stream(0), write_stream(true)
00252 #endif
00253    {}
00254 
00255 #ifdef COIN_COMPILE_WITH_TRACING
00256 
00258    CoinTimer(std::fstream* s, bool write) :
00259       start(0), limit(1e100), end(1e100),
00260       stream(s), write_stream(write) {}
00261    
00264    CoinTimer(double lim, std::fstream* s, bool w) :
00265       start(CoinCpuTime()), limit(lim), end(start+lim),
00266       stream(s), write_stream(w) {}
00267 #endif
00268    
00270    inline void restart() { start=CoinCpuTime(); end=start+limit; }
00272    inline void reset() { restart(); }
00274    inline void reset(double lim) { limit=lim; restart(); }
00275 
00278    inline bool isPastPercent(double pct) const {
00279       return evaluate(start + limit * pct < CoinCpuTime());
00280    }
00283    inline bool isPast(double lim) const {
00284       return evaluate(start + lim < CoinCpuTime());
00285    }
00288    inline bool isExpired() const {
00289       return evaluate(end < CoinCpuTime());
00290    }
00291 
00293    inline double timeLeft() const {
00294       return evaluate(end - CoinCpuTime());
00295    }
00296 
00298    inline double timeElapsed() const {
00299       return evaluate(CoinCpuTime() - start);
00300    }
00301 
00302    inline void setLimit(double l) {
00303       limit = l;
00304       return;
00305    }
00306 };
00307 
00308 #endif

Generated on Fri Apr 17 03:06:14 2009 by  doxygen 1.4.7