00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef GLVU_STOPWATCH_INCLUDED
00039 #define GLVU_STOPWATCH_INCLUDED
00040
00041
00042 #include <time.h>
00043 #include <sys/timeb.h>
00044 #ifndef _WIN32
00045 #include <sys/time.h>
00046 #else
00047 #include <windows.h>
00048 #endif
00049
00050 #define STOPWATCH_MAX_NAME 40
00051
00052
00053
00054
00055 class StopwatchBase
00056 {
00057 public:
00058 explicit StopwatchBase(const char *name=0);
00059 virtual ~StopwatchBase();
00060 inline void Start();
00061 inline void Stop();
00062 inline void Reset();
00063 inline float GetTime() const;
00064 float GetAvgTime() const;
00065 int GetNumStarts() const;
00066 void SetName(const char *n);
00067 void SetName(int id);
00068 const char* GetName() const;
00069 const char* GetType() const;
00070
00071 protected:
00072 float elapsedTime;
00073 int numStarts;
00074 char sw_name[STOPWATCH_MAX_NAME];
00075 const char *sw_type;
00076 bool running;
00077
00078
00079 virtual void markTime()=0;
00080 virtual float diffTime() const =0;
00081 };
00082
00083
00084
00085
00086
00087
00088 template <class _OSTREAM>
00089 inline _OSTREAM& operator<< (_OSTREAM& o, const StopwatchBase& s)
00090 {
00091 if (s.GetNumStarts() > 1)
00092 o << s.GetName() << " avg time: "
00093 << s.GetAvgTime() << " sec, (avg of " << s.GetNumStarts() << " periods)";
00094 else
00095 o << s.GetName() << " time: " << s.GetTime() << " sec";
00096 return o;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105 class StopwatchGeneric : public StopwatchBase
00106 {
00107 public:
00108 StopwatchGeneric(const char* name=0);
00109 virtual ~StopwatchGeneric();
00110 protected:
00111 void markTime();
00112 float diffTime() const;
00113 struct timeb lastStamp;
00114 };
00115
00116 #ifdef _WIN32
00117 class StopwatchWin32 : public StopwatchBase
00118 {
00119 public:
00120 StopwatchWin32(const char* name=0);
00121 virtual ~StopwatchWin32();
00122 protected:
00123 void markTime();
00124 float diffTime() const;
00125 LARGE_INTEGER lastStamp;
00126 static LARGE_INTEGER clockFreq;
00127 static bool clockFreqSet;
00128 };
00129 #endif
00130
00131 class CPUStopwatchGeneric : public StopwatchBase
00132 {
00133 public:
00134 CPUStopwatchGeneric(const char* name=0);
00135 virtual ~CPUStopwatchGeneric();
00136 protected:
00137 void markTime();
00138 float diffTime() const;
00139 clock_t lastStamp;
00140 };
00141
00142 #ifndef _WIN32
00143 class StopwatchGTOD : public StopwatchBase
00144 {
00145 public:
00146 StopwatchGTOD(const char* name=0);
00147 virtual ~StopwatchGTOD();
00148 protected:
00149 void markTime();
00150 float diffTime() const;
00151 struct timeval lastStamp;
00152 };
00153 #endif
00154
00155 #ifdef CLOCK_SGI_CYCLE
00156 class StopwatchSGI : public StopwatchBase
00157 {
00158 public:
00159 StopwatchSGI(const char* name=0);
00160 virtual ~StopwatchSGI();
00161 protected:
00162 void markTime();
00163 float diffTime() const;
00164 timespec_t lastStamp;
00165 };
00166 #endif
00167
00168
00169
00170
00171
00172 typedef CPUStopwatchGeneric CPUStopwatch;
00173
00174 #ifdef _WIN32
00175
00176 typedef StopwatchWin32 Stopwatch;
00177 #else
00178 #ifdef CLOCK_SGI_CYCLE
00179 typedef StopwatchSGI Stopwatch;
00180 #else
00181 typedef StopwatchGTOD Stopwatch;
00182 #endif
00183 #endif
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 void StopwatchBase::Start()
00194 {
00195 if (running) {
00196 numStarts++;
00197 return;
00198 }
00199 running = true;
00200 numStarts++;
00201
00202
00203 markTime();
00204 }
00205
00206 void StopwatchBase::Stop()
00207 {
00208 if (running) {
00209 elapsedTime += diffTime();
00210 running = false;
00211 }
00212 }
00213
00214 void StopwatchBase::Reset()
00215 {
00216 if (running) { markTime(); numStarts=1; }
00217 else numStarts = 0;
00218 elapsedTime = 0.0f;
00219 }
00220
00221 float StopwatchBase::GetTime() const
00222 {
00223 if (running) return (elapsedTime + diffTime());
00224 else return (elapsedTime);
00225 }
00226
00227
00228
00229
00230 #endif