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

- File List
- File Members

# /home/coin/SVN-release/Cgl-0.52.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 static inline double CoinCpuTime()
    00094 {
    00095   double cpu_temp;
    00096 #if defined(_MSC_VER) || defined(__MSVCRT__)
    00097   unsigned int ticksnow;        /* clock_t is same as int */
    00098
    00099   ticksnow = (unsigned int)clock();
    00100
    00101   cpu_temp = (double)((double)ticksnow/CLOCKS_PER_SEC);
    00102 #else
    00103   struct rusage usage;
    00104 # ifdef ZEROFAULT
    00105   usage.ru_utime.tv_sec = 0 ;
    00106   usage.ru_utime.tv_usec = 0 ;
    00107 # endif
    00108   getrusage(RUSAGE_SELF,&usage);
    00109   cpu_temp = usage.ru_utime.tv_sec;
    00110   cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
    00111 #endif
    00112   return cpu_temp;
    00113 }
    00114
    00115 //#############################################################################
    00116 // On most systems SELF seems to include children threads, This is for when it doesn't
    00117 static inline double CoinCpuTimeJustChildren()
    00118 {
    00119   double cpu_temp;
    00120 #if defined(_MSC_VER) || defined(__MSVCRT__)
    00121   cpu_temp = 0.0;
    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_CHILDREN,&usage);
    00129   cpu_temp = usage.ru_utime.tv_sec;
    00130   cpu_temp += 1.0e-6*((double) usage.ru_utime.tv_usec);
    00131 #endif
    00132   return cpu_temp;
    00133 }
    00134 //#############################################################################
    00135
    00136 #include <fstream>
    00137
    00153 class CoinTimer
    00154 {
    00155 private:
    00157    double start;
    00159    double limit;
    00160    double end;
    00161 #ifdef COIN_COMPILE_WITH_TRACING
    00162    std::fstream* stream;
    00163    bool write_stream;
    00164 #endif
    00165
    00166 private:
    00167 #ifdef COIN_COMPILE_WITH_TRACING
    00168    inline bool evaluate(bool b_tmp) const {
    00169       int i_tmp = b_tmp;
    00170       if (stream) {
    00171          if (write_stream)
    00172             (*stream) << i_tmp << "\n";
    00173          else
    00174             (*stream) >> i_tmp;
    00175       }
    00176       return i_tmp;
    00177    }
    00178    inline double evaluate(double d_tmp) const {
    00179       if (stream) {
    00180          if (write_stream)
    00181             (*stream) << d_tmp << "\n";
    00182          else
    00183             (*stream) >> d_tmp;
    00184       }
    00185       return d_tmp;
    00186    }
    00187 #else
    00188    inline bool evaluate(const bool b_tmp) const {
    00189       return b_tmp;
    00190    }
    00191    inline double evaluate(const double d_tmp) const {
    00192       return d_tmp;
    00193    }
    00194 #endif
    00195
    00196 public:
    00198    CoinTimer() :
    00199       start(0), limit(1e100), end(1e100)
    00200 #ifdef COIN_COMPILE_WITH_TRACING
    00201       , stream(0), write_stream(true)
    00202 #endif
    00203    {}
    00204
    00206    CoinTimer(double lim) :
    00207       start(CoinCpuTime()), limit(lim), end(start+lim)
    00208 #ifdef COIN_COMPILE_WITH_TRACING
    00209       , stream(0), write_stream(true)
    00210 #endif
    00211    {}
    00212
    00213 #ifdef COIN_COMPILE_WITH_TRACING
    00214
    00216    CoinTimer(std::fstream* s, bool write) :
    00217       start(0), limit(1e100), end(1e100),
    00218       stream(s), write_stream(write) {}
    00219
    00222    CoinTimer(double lim, std::fstream* s, bool w) :
    00223       start(CoinCpuTime()), limit(lim), end(start+lim),
    00224       stream(s), write_stream(w) {}
    00225 #endif
    00226
    00228    inline void restart() { start=CoinCpuTime(); end=start+limit; }
    00230    inline void reset() { restart(); }
    00232    inline void reset(double lim) { limit=lim; restart(); }
    00233
    00236    inline bool isPastPercent(double pct) const {
    00237       return evaluate(start + limit * pct < CoinCpuTime());
    00238    }
    00241    inline bool isPast(double lim) const {
    00242       return evaluate(start + lim < CoinCpuTime());
    00243    }
    00246    inline bool isExpired() const {
    00247       return evaluate(end < CoinCpuTime());
    00248    }
    00249
    00251    inline double timeLeft() const {
    00252       return evaluate(end - CoinCpuTime());
    00253    }
    00254
    00256    inline double timeElapsed() const {
    00257       return evaluate(CoinCpuTime() - start);
    00258    }
    00259
    00260    inline void setLimit(double l) {
    00261       limit = l;
    00262       return;
    00263    }
    00264 };
    00265
    00266 #endif