00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00019
00020
00021 #ifndef _TIMER_H_
00022 #define _TIMER_H_
00023
00024 #ifdef __SVR4 // Solaris
00025 #include <sys/time.h>
00026 #include <strstream.h>
00027 #include <unistd.h>
00028 #include <fcntl.h>
00029 #include <sys/procfs.h>
00030 #include <stdio.h>
00031 #endif // __SVR4
00032
00033 #include <time.h>
00034
00035 #if defined(unix) || defined(__linux)
00036 #include <sys/time.h>
00037 #include <unistd.h>
00038 #endif
00039
00040
00041 #ifdef __sgi
00042 #include <sys/types.h>
00043 #include <sys/times.h>
00044 #include <limits.h>
00045 #endif
00046
00047
00048 #ifdef WIN32
00049 #include <windows.h>
00050 #endif
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 class Timer {
00074
00075 public:
00076
00077 Timer (void)
00078 : _starttime (0),
00079 _elapsedtime (0)
00080 {}
00081
00082
00083 void start (void) { _starttime = _time(); }
00084
00085
00086 void stop (void) { _elapsedtime = _time() - _starttime; }
00087
00088
00089 void reset (void) { _starttime = _elapsedtime = 0; }
00090
00091
00092 void set (double secs) { _starttime = 0; _elapsedtime = _sectotime (secs);}
00093
00094
00095 operator double (void) { return _timetosec (_elapsedtime); }
00096
00097
00098 private:
00099
00100
00101
00102
00103 #ifdef __sgi
00104 #define TIMER_FOUND
00105
00106 long _starttime, _elapsedtime;
00107
00108 long _time (void) {
00109 struct tms t;
00110 long ticks = times (&t);
00111 return ticks;
00112 }
00113
00114 double _timetosec (long t) {
00115 return ((double) (t) / CLK_TCK);
00116 }
00117
00118 long _sectotime (double sec) {
00119 return (long) sec * CLK_TCK;
00120 }
00121 #endif
00122
00123 #ifdef __SVR4 // Solaris
00124 #define TIMER_FOUND
00125 hrtime_t _starttime, _elapsedtime;
00126
00127 virtual hrtime_t _time (void) {
00128 return gethrtime();
00129 }
00130
00131 hrtime_t _sectotime (double sec) { return (hrtime_t) (sec * 1.0e9); }
00132
00133 double _timetosec (hrtime_t& t) {
00134 return ((double) (t) / 1.0e9);
00135 }
00136 #endif // __SVR4
00137
00138 #if defined(MAC) || defined(macintosh)
00139 #define TIMER_FOUND
00140 double _starttime, _elapsedtime;
00141
00142 double _time (void) {
00143 return get_Mac_microseconds();
00144 }
00145
00146 double _timetosec (hrtime_t& t) {
00147 return t;
00148 }
00149 #endif // MAC
00150
00151 #ifdef WIN32
00152 #define TIMER_FOUND
00153 DWORD _starttime, _elapsedtime;
00154
00155 DWORD _time (void) {
00156 return GetTickCount();
00157 }
00158
00159 double _timetosec (DWORD& t) {
00160 return (double) t / 1000.0;
00161 }
00162
00163 unsigned long _sectotime (double sec) {
00164 return sec * 1000;
00165 }
00166
00167 #endif // WIN32
00168
00169 #ifndef TIMER_FOUND
00170
00171 long _starttime, _elapsedtime;
00172
00173 long _time (void) {
00174 struct timeval t;
00175 struct timezone tz;
00176 gettimeofday (&t, &tz);
00177 return t.tv_sec * 1000000 + t.tv_usec;
00178 }
00179
00180 double _timetosec (long t) {
00181 return ((double) (t) / 1000000);
00182 }
00183
00184 long _sectotime (double sec) {
00185 return (long) sec * 1000000;
00186 }
00187
00188 #endif // TIMER_FOUND
00189
00190 #undef TIMER_FOUND
00191
00192 };
00193
00194
00195 #ifdef __SVR4 // Solaris
00196 class VirtualTimer : public Timer {
00197 public:
00198 hrtime_t _time (void) {
00199 return gethrvtime();
00200 }
00201 };
00202 #endif
00203
00204 #endif