Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

timer.h

00001 // Emacs will be in -*- Mode: c++ -*-
00003 //
00004 // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
00005 //        for Shared-Memory Multiprocessors
00006 // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
00007 //
00008 // Copyright (c) 1998-2000, The University of Texas at Austin.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Library General Public License as
00012 // published by the Free Software Foundation, http://www.fsf.org.
00013 //
00014 // This library is distributed in the hope that it will be useful, but
00015 // WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Library General Public License for more details.
00018 //
00020 
00021 
00022 #ifndef _TIMER_H_
00023 #define _TIMER_H_
00024 
00025 #ifdef __SVR4 // Solaris
00026 #include <sys/time.h>
00027 #include <strstream.h>
00028 #include <unistd.h>
00029 #include <fcntl.h>
00030 #include <sys/procfs.h>
00031 #include <stdio.h>
00032 #endif // __SVR4
00033 
00034 #include <time.h>
00035 
00036 #if defined(unix) || defined(__linux)
00037 #include <sys/time.h>
00038 #include <unistd.h>
00039 #endif
00040 
00041 
00042 #ifdef __sgi
00043 #include <sys/types.h>
00044 #include <sys/times.h>
00045 #include <limits.h>
00046 #endif
00047 
00048 
00049 #if defined(WIN32) || defined(__WIN32__) || defined(_WIN32)
00050 #ifndef WIN32FLAG
00051 #define WIN32FLAG 1
00052 #endif
00053 #include <windows.h>
00054 #endif
00055 
00056 #if defined(__BEOS__)
00057 #include <OS.h>
00058 #endif
00059 
00081 class Timer {
00082 
00083 public:
00085   Timer (void)
00086     : _starttime (0),
00087       _elapsedtime (0)
00088     {}
00089 
00091   void start (void) { _starttime = _time(); }
00092 
00094   void stop (void) { _elapsedtime += _time() - _starttime; }
00095 
00097   void reset (void) { _starttime = _elapsedtime = 0; }
00098 
00100   void set (double secs) { _starttime = 0; _elapsedtime = _sectotime (secs);}
00101 
00103   operator double (void) { return _timetosec (_elapsedtime); }
00104 
00105 
00106 private:
00107 
00110 
00111 #ifdef __sgi
00112 #define TIMER_FOUND
00113 
00114   long _starttime, _elapsedtime;
00115 
00116   long _time (void) {
00117     struct tms t;
00118     long ticks = times (&t);
00119     return ticks;
00120   }
00121 
00122   double _timetosec (long t) {
00123     return ((double) (t) / CLK_TCK);
00124   }
00125 
00126   long _sectotime (double sec) {
00127     return (long) sec * CLK_TCK;
00128   }
00129 #endif
00130 
00131 #ifdef __SVR4 // Solaris
00132 #define TIMER_FOUND
00133   hrtime_t  _starttime, _elapsedtime;
00134 
00135   virtual hrtime_t _time (void) {
00136     return gethrtime();
00137   }
00138 
00139   hrtime_t _sectotime (double sec) { return (hrtime_t) (sec * 1.0e9); }
00140 
00141   double _timetosec (hrtime_t& t) {
00142     return ((double) (t) / 1.0e9);
00143   }
00144 #endif // __SVR4
00145 
00146 #if defined(MAC) || defined(macintosh)
00147 #define TIMER_FOUND
00148   double        _starttime, _elapsedtime;
00149 
00150   double _time (void) {
00151     return get_Mac_microseconds();
00152   }
00153 
00154   double _timetosec (hrtime_t& t) {
00155     return t;
00156   }
00157 #endif // MAC
00158 
00159 #ifdef WIN32FLAG
00160 #define TIMER_FOUND
00161   DWORD _starttime, _elapsedtime;
00162 
00163   DWORD _time (void) {
00164     return GetTickCount();
00165   }
00166 
00167   double _timetosec (DWORD& t) {
00168     return (double) t / 1000.0;
00169   }
00170 
00171   unsigned long _sectotime (double sec) {
00172         return (unsigned long)(sec * 1000);
00173     }
00174 
00175 #endif // WIN32
00176 
00177 
00178 #ifdef __BEOS__
00179 #define TIMER_FOUND
00180   bigtime_t _starttime, _elapsedtime;
00181   bigtime_t _time(void) {
00182     return system_time();
00183   }
00184   double _timetosec (bigtime_t& t) {
00185     return (double) t / 1000000.0;
00186   }
00187   
00188   bigtime_t _sectotime (double sec) {
00189     return (bigtime_t)(sec * 1000000.0);
00190   }
00191 #endif // __BEOS__
00192 
00193 #ifndef TIMER_FOUND
00194 
00195   long _starttime, _elapsedtime;
00196 
00197   long _time (void) {
00198     struct timeval t;
00199     struct timezone tz;
00200     gettimeofday (&t, &tz);
00201     return t.tv_sec * 1000000 + t.tv_usec;
00202   }
00203 
00204   double _timetosec (long t) {
00205     return ((double) (t) / 1000000);
00206   }
00207 
00208   long _sectotime (double sec) {
00209     return (long) sec * 1000000;
00210   }
00211 
00212 #endif // TIMER_FOUND
00213 
00214 #undef TIMER_FOUND
00215 
00216 };
00217 
00218 
00219 #ifdef __SVR4 // Solaris
00220 class VirtualTimer : public Timer {
00221 public:
00222   hrtime_t _time (void) {
00223     return gethrvtime();
00224   }
00225 };  
00226 #endif
00227 
00228 #endif

Generated at Wed Nov 7 16:25:59 2001 for Optimization by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001