- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# /home/coin/SVN-release/Cgl-0.55.0/CoinUtils/src/CoinTime.hpp

Go to the documentation of this file.

    00001 /* $Id: CoinTime.hpp 1239 2009-12-10 16:16:11Z ladanyi $ */
    00002 // Copyright (C) 2002, International Business Machines
    00003 // Corporation and others.  All Rights Reserved.
    00004
    00005 #ifndef _CoinTime_hpp
    00006 #define _CoinTime_hpp
    00007
    00008 // Uncomment the next three lines for thorough memory initialisation.
    00009 // #ifndef ZEROFAULT
    00010 // # define ZEROFAULT
    00011 // #endif
    00012
    00013 //#############################################################################
    00014
    00015 #include <ctime>
    00016 #if defined(_MSC_VER)
    00017 // Turn off compiler warning about long names
    00018 #  pragma warning(disable:4786)
    00019 #else
    00020 // MacOS-X and FreeBSD needs sys/time.h
    00021 #if defined(__MACH__) || defined (__FreeBSD__)
    00022 #include <sys/time.h>
    00023 #endif
    00024 #if !defined(__MSVCRT__)
    00025 #include <sys/resource.h>
    00026 #endif
    00027 #endif
    00028
    00029 //#############################################################################
    00030
    00031 #if defined(_MSC_VER)
    00032
    00033 #if 0 // change this to 1 if want to use the win32 API
    00034 #include <windows.h>
    00035 #ifdef small
    00036 /* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
    00037    '#define small char' */
    00038 #undef small
    00039 #endif
    00040 #define TWO_TO_THE_THIRTYTWO 4294967296.0
    00041 #define DELTA_EPOCH_IN_SECS  11644473600.0
    00042 inline double CoinGetTimeOfDay()
    00043 {
    00044   FILETIME ft;
    00045
    00046   GetSystemTimeAsFileTime(&ft);
    00047   double t = ft.dwHighDateTime * TWO_TO_THE_THIRTYTWO + ft.dwLowDateTime;
    00048   t = t/10000000.0 - DELTA_EPOCH_IN_SECS;
    00049   return t;
    00050 }
    00051 #else
    00052 #include <sys/types.h>
    00053 #include <sys/timeb.h>
    00054 inline double CoinGetTimeOfDay()
    00055 {
    00056   struct _timeb timebuffer;
    00057 #pragma warning(disable:4996)
    00058   _ftime( &timebuffer ); // C4996
    00059 #pragma warning(default:4996)
    00060   return timebuffer.time + timebuffer.millitm/1000.0;
    00061 }
    00062 #endif
    00063
    00064 #else
    00065
    00066 #include <sys/time.h>
    00067
    00068 inline double CoinGetTimeOfDay()
    00069 {
    00070     struct timeval tv;
    00071     gettimeofday(&tv, NULL);
    00072     return static_cast<double>(tv.tv_sec) + static_cast<int>(tv.tv_usec)/1000000.0;
    00073 }
    00074
    00075 #endif // _MSC_VER
    00076
    00085 inline double CoinWallclockTime(double callType = 0)
    00086 {
    00087     double callTime = CoinGetTimeOfDay();
    00088     static const double firstCall = callType > 0 ? callType : callTime;
    00089     return callType < 0 ? firstCall : callTime - firstCall;
    00090 }
    00091
    00092 //#############################################################################
    00093
    00094 //#define HAVE_SDK // if SDK under Win32 is installed, for CPU instead of elapsed time under Win
    00095 #ifdef HAVE_SDK
    00096 #include <windows.h>
    00097 #ifdef small
    00098 /* for some unfathomable reason (to me) rpcndr.h (pulled in by windows.h) does a
    00099    '#define small char' */
    00100 #undef small
    00101 #endif
    00102 #define TWO_TO_THE_THIRTYTWO 4294967296.0
    00103 #endif
    00104
    00105 static inline double CoinCpuTime()
    00106 {
    00107   double cpu_temp;
    00108 #if defined(_MSC_VER) || defined(__MSVCRT__)
    00109 #ifdef HAVE_SDK
    00110   FILETIME creation;
    00111   FILETIME exit;
    00112   FILETIME kernel;
    00113   FILETIME user;
    00114   GetProcessTimes(GetCurrentProcess(), &creation, &exit, &kernel, &user);
    00115   double t = user.dwHighDateTime * TWO_TO_THE_THIRTYTWO + user.dwLowDateTime;
    00116   return t/10000000.0;
    00117 #else
    00118   unsigned int ticksnow;        /* clock_t is same as int */
    00119   ticksnow = (unsigned int)clock();
    00120   cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
    00121 #endif
    00122
    00123 #else
    00124   struct rusage usage;
    00125 # ifdef ZEROFAULT
    00126   usage.ru_utime.tv_sec = 0 ;
    00127   usage.ru_utime.tv_usec = 0 ;
    00128 # endif
    00129   getrusage(RUSAGE_SELF,&usage);
    00130   cpu_temp = static_cast<double>(usage.ru_utime.tv_sec);
    00131   cpu_temp += 1.0e-6*(static_cast<double> (usage.ru_utime.tv_usec));
    00132 #endif
    00133   return cpu_temp;
    00134 }
    00135
    00136 //#############################################################################
    00137
    00138
    00139
    00140 static inline double CoinSysTime()
    00141 {
    00142   double sys_temp;
    00143 #if defined(_MSC_VER) || defined(__MSVCRT__)
    00144   sys_temp = 0.0;
    00145 #else
    00146   struct rusage usage;
    00147 # ifdef ZEROFAULT
    00148   usage.ru_utime.tv_sec = 0 ;
    00149   usage.ru_utime.tv_usec = 0 ;
    00150 # endif
    00151   getrusage(RUSAGE_SELF,&usage);
    00152   sys_temp = static_cast<double>(usage.ru_stime.tv_sec);
    00153   sys_temp += 1.0e-6*(static_cast<double> (usage.ru_stime.tv_usec));
    00154 #endif
    00155   return sys_temp;
    00156 }
    00157
    00158 //#############################################################################
    00159 // On most systems SELF seems to include children threads, This is for when it doesn't
    00160 static inline double CoinCpuTimeJustChildren()
    00161 {
    00162   double cpu_temp;
    00163 #if defined(_MSC_VER) || defined(__MSVCRT__)
    00164   cpu_temp = 0.0;
    00165 #else
    00166   struct rusage usage;
    00167 # ifdef ZEROFAULT
    00168   usage.ru_utime.tv_sec = 0 ;
    00169   usage.ru_utime.tv_usec = 0 ;
    00170 # endif
    00171   getrusage(RUSAGE_CHILDREN,&usage);
    00172   cpu_temp = static_cast<double>(usage.ru_utime.tv_sec);
    00173   cpu_temp += 1.0e-6*(static_cast<double> (usage.ru_utime.tv_usec));
    00174 #endif
    00175   return cpu_temp;
    00176 }
    00177 //#############################################################################
    00178
    00179 #include <fstream>
    00180
    00196 class CoinTimer
    00197 {
    00198 private:
    00200    double start;
    00202    double limit;
    00203    double end;
    00204 #ifdef COIN_COMPILE_WITH_TRACING
    00205    std::fstream* stream;
    00206    bool write_stream;
    00207 #endif
    00208
    00209 private:
    00210 #ifdef COIN_COMPILE_WITH_TRACING
    00211    inline bool evaluate(bool b_tmp) const {
    00212       int i_tmp = b_tmp;
    00213       if (stream) {
    00214          if (write_stream)
    00215             (*stream) << i_tmp << "\n";
    00216          else
    00217             (*stream) >> i_tmp;
    00218       }
    00219       return i_tmp;
    00220    }
    00221    inline double evaluate(double d_tmp) const {
    00222       if (stream) {
    00223          if (write_stream)
    00224             (*stream) << d_tmp << "\n";
    00225          else
    00226             (*stream) >> d_tmp;
    00227       }
    00228       return d_tmp;
    00229    }
    00230 #else
    00231    inline bool evaluate(const bool b_tmp) const {
    00232       return b_tmp;
    00233    }
    00234    inline double evaluate(const double d_tmp) const {
    00235       return d_tmp;
    00236    }
    00237 #endif
    00238
    00239 public:
    00241    CoinTimer() :
    00242       start(0), limit(1e100), end(1e100)
    00243 #ifdef COIN_COMPILE_WITH_TRACING
    00244       , stream(0), write_stream(true)
    00245 #endif
    00246    {}
    00247
    00249    CoinTimer(double lim) :
    00250       start(CoinCpuTime()), limit(lim), end(start+lim)
    00251 #ifdef COIN_COMPILE_WITH_TRACING
    00252       , stream(0), write_stream(true)
    00253 #endif
    00254    {}
    00255
    00256 #ifdef COIN_COMPILE_WITH_TRACING
    00257
    00259    CoinTimer(std::fstream* s, bool write) :
    00260       start(0), limit(1e100), end(1e100),
    00261       stream(s), write_stream(write) {}
    00262
    00265    CoinTimer(double lim, std::fstream* s, bool w) :
    00266       start(CoinCpuTime()), limit(lim), end(start+lim),
    00267       stream(s), write_stream(w) {}
    00268 #endif
    00269
    00271    inline void restart() { start=CoinCpuTime(); end=start+limit; }
    00273    inline void reset() { restart(); }
    00275    inline void reset(double lim) { limit=lim; restart(); }
    00276
    00279    inline bool isPastPercent(double pct) const {
    00280       return evaluate(start + limit * pct < CoinCpuTime());
    00281    }
    00284    inline bool isPast(double lim) const {
    00285       return evaluate(start + lim < CoinCpuTime());
    00286    }
    00289    inline bool isExpired() const {
    00290       return evaluate(end < CoinCpuTime());
    00291    }
    00292
    00294    inline double timeLeft() const {
    00295       return evaluate(end - CoinCpuTime());
    00296    }
    00297
    00299    inline double timeElapsed() const {
    00300       return evaluate(CoinCpuTime() - start);
    00301    }
    00302
    00303    inline void setLimit(double l) {
    00304       limit = l;
    00305       return;
    00306    }
    00307 };
    00308
    00309 #endif