00001
00002
00003
00004
00005
00006 #include "HWTimer.h"
00007
00008 #if defined(WIN32)
00009
00010 HWTimer::HWTimer()
00011 {
00012 LARGE_INTEGER freq;
00013
00014 if (QueryPerformanceFrequency(&freq))
00015 {
00016 m_IsSupported = true;
00017 m_FloatFreq = (float)freq.QuadPart;
00018 m_DoubleFreq = (double)freq.QuadPart;
00019 }
00020 else
00021 {
00022 m_IsSupported = false;
00023 m_FloatFreq = 1.0f;
00024 m_DoubleFreq = 1.0;
00025 }
00026 }
00027
00028 HWTimer::HWTimer(HWTimer& t)
00029 {
00030 m_LastCount = t.m_LastCount;
00031 m_FloatFreq = t.m_FloatFreq;
00032 m_DoubleFreq = t.m_DoubleFreq;
00033 m_IsSupported = t.m_IsSupported;
00034 }
00035
00036 HWTimer::~HWTimer()
00037 {
00038 }
00039
00040 bool HWTimer::IsSupported(void)
00041 {
00042 return m_IsSupported;
00043 }
00044
00045 float HWTimer::GetElapsedTimeFloat(void)
00046 {
00047 LARGE_INTEGER count;
00048 float time;
00049
00050 QueryPerformanceCounter(&count);
00051 time = (float)(count.QuadPart - m_LastCount.QuadPart)/m_FloatFreq;
00052 m_LastCount = count;
00053
00054 return time;
00055 }
00056
00057 double HWTimer::GetElapsedTimeDouble(void)
00058 {
00059 LARGE_INTEGER count;
00060 double time;
00061
00062 QueryPerformanceCounter(&count);
00063 time = (double)(count.QuadPart - m_LastCount.QuadPart)/m_DoubleFreq;
00064 m_LastCount = count;
00065
00066 return time;
00067 }
00068
00069 void HWTimer::SleepFloat(float s)
00070 {
00071 LARGE_INTEGER count;
00072 float time = -1.0f;
00073
00074 if (s < 0.0f)
00075 return;
00076
00077 while(time < s)
00078 {
00079 QueryPerformanceCounter(&count);
00080 time = (float)(count.QuadPart - m_LastCount.QuadPart)/m_FloatFreq;
00081 }
00082 m_LastCount = count;
00083
00084 return;
00085 }
00086
00087 void HWTimer::SleepDouble(double s)
00088 {
00089 LARGE_INTEGER count;
00090 double time = -1.0f;
00091
00092 if (s < 0.0f)
00093 return;
00094
00095 while(time < s)
00096 {
00097 QueryPerformanceCounter(&count);
00098 time = (double)(count.QuadPart - m_LastCount.QuadPart)/m_DoubleFreq;
00099 }
00100 m_LastCount = count;
00101
00102 return;
00103 }
00104
00105 HWTimer& HWTimer::operator=(HWTimer& rhs)
00106 {
00107 if (this != &rhs)
00108 {
00109 m_LastCount = rhs.m_LastCount;
00110 m_FloatFreq = rhs.m_FloatFreq;
00111 m_DoubleFreq = rhs.m_DoubleFreq;
00112 m_IsSupported = rhs.m_IsSupported;
00113 }
00114 return *this;
00115 }
00116
00117 #else
00118
00119 #define TIMEVALDIFFFLOAT(t1, t2) \
00120 (((((float)(t1.tv_sec-t2.tv_sec))*1000000.0f) + \
00121 ((float)(t1.tv_usec-t2.tv_usec)))/1000000.0f)
00122 #define TIMEVALDIFFDOUBLE(t1, t2) \
00123 (((((double)(t1.tv_sec-t2.tv_sec))*1000000.0) + \
00124 ((double)(t1.tv_usec-t2.tv_usec)))/1000000.0)
00125
00126 HWTimer::HWTimer()
00127 {
00128 struct timeval tv;
00129
00130 m_IsSupported = (gettimeofday(&tv, 0) == 0);
00131 }
00132
00133 HWTimer::HWTimer(HWTimer& t)
00134 {
00135 m_LastCount.tv_sec = t.m_LastCount.tv_sec;
00136 m_LastCount.tv_usec = t.m_LastCount.tv_usec;
00137 m_IsSupported = t.m_IsSupported;
00138 }
00139
00140 HWTimer::~HWTimer()
00141 {
00142 }
00143
00144 bool HWTimer::IsSupported(void)
00145 {
00146 return m_IsSupported;
00147 }
00148
00149 float HWTimer::GetElapsedTimeFloat(void)
00150 {
00151 struct timeval count;
00152 float time;
00153
00154 gettimeofday(&count, 0);
00155 time = TIMEVALDIFFFLOAT(count, m_LastCount);
00156 m_LastCount.tv_sec = count.tv_sec;
00157 m_LastCount.tv_usec = count.tv_usec;
00158
00159 return time;
00160 }
00161
00162 double HWTimer::GetElapsedTimeDouble(void)
00163 {
00164 struct timeval count;
00165 double time;
00166
00167 gettimeofday(&count, 0);
00168 time = TIMEVALDIFFDOUBLE(count, m_LastCount);
00169 m_LastCount.tv_sec = count.tv_sec;
00170 m_LastCount.tv_usec = count.tv_usec;
00171
00172 return time;
00173 }
00174
00175 void HWTimer::SleepFloat(float s)
00176 {
00177 struct timeval count;
00178 float time = -1.0f;
00179
00180 if (s < 0.0f)
00181 return;
00182
00183 while(time < s)
00184 {
00185 gettimeofday(&count, 0);
00186 time = TIMEVALDIFFFLOAT(count, m_LastCount);
00187 }
00188 m_LastCount.tv_sec = count.tv_sec;
00189 m_LastCount.tv_usec = count.tv_usec;
00190
00191 return;
00192 }
00193
00194 void HWTimer::SleepDouble(double s)
00195 {
00196 struct timeval count;
00197 double time = -1.0f;
00198
00199 if (s < 0.0f)
00200 return;
00201
00202 while(time < s)
00203 {
00204 gettimeofday(&count, 0);
00205 time = TIMEVALDIFFDOUBLE(count, m_LastCount);
00206 }
00207 m_LastCount.tv_sec = count.tv_sec;
00208 m_LastCount.tv_usec = count.tv_usec;
00209
00210 return;
00211 }
00212
00213 HWTimer& HWTimer::operator=(HWTimer& rhs)
00214 {
00215 if (this != &rhs)
00216 {
00217 m_LastCount.tv_sec = rhs.m_LastCount.tv_sec;
00218 m_LastCount.tv_usec = rhs.m_LastCount.tv_usec;
00219 m_IsSupported = rhs.m_IsSupported;
00220 }
00221 return *this;
00222 }
00223
00224 #endif