clock.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef EQBASE_CLOCK_H
00020 #define EQBASE_CLOCK_H
00021
00022 #include <eq/base/base.h>
00023
00024 #ifdef Darwin
00025
00026 # include <mach/mach_time.h>
00027 #endif
00028
00029 #ifdef WIN32
00030 # include <Windows.h>
00031 #else
00032 # include <time.h>
00033 #endif
00034
00035 namespace eq
00036 {
00037 namespace base
00038 {
00040 class Clock
00041 {
00042 public :
00044 Clock()
00045 {
00046 reset();
00047 #ifdef Darwin
00048 mach_timebase_info( &_timebaseInfo );
00049 #elif defined (WIN32)
00050 QueryPerformanceFrequency( &_frequency );
00051 #endif
00052 }
00053
00055 ~Clock() {}
00056
00061 void reset()
00062 {
00063 #ifdef Darwin
00064 _start = mach_absolute_time();
00065 #elif defined (WIN32)
00066 QueryPerformanceCounter( &_start );
00067 #else
00068 clock_gettime( CLOCK_REALTIME, &_start );
00069 #endif
00070 }
00071
00073 void set( const int64_t time )
00074 {
00075 reset();
00076 #ifdef Darwin
00077 _start -= static_cast< uint64_t >(
00078 time * _timebaseInfo.denom / _timebaseInfo.numer *
00079 1000000 );
00080 #elif defined (WIN32)
00081 _start.QuadPart -= static_cast<long long>(
00082 time * _frequency.QuadPart / 1000 );
00083 #else
00084 const int sec = static_cast< int >( time / 1000 ) + 1;
00085 _start.tv_sec -= sec;
00086 _start.tv_nsec -= static_cast<int>(
00087 (time - sec * 1000) * 1000000 );
00088 if( _start.tv_nsec > 1000000000 )
00089 {
00090 _start.tv_sec += 1;
00091 _start.tv_nsec -= 1000000000;
00092 }
00093 #endif
00094 }
00095
00100 float getTimef() const
00101 {
00102 #ifdef Darwin
00103 const int64_t elapsed = mach_absolute_time() - _start;
00104 return ( elapsed * _timebaseInfo.numer / _timebaseInfo.denom /
00105 1000000.f );
00106 #elif defined (WIN32)
00107 LARGE_INTEGER now;
00108 QueryPerformanceCounter( &now );
00109 return 1000.0f * (now.QuadPart - _start.QuadPart) /
00110 _frequency.QuadPart;
00111 #else
00112 struct timespec now;
00113 clock_gettime( CLOCK_REALTIME, &now );
00114 return ( 1000.0f * (now.tv_sec - _start.tv_sec) +
00115 0.000001f * (now.tv_nsec - _start.tv_nsec));
00116 #endif
00117 }
00118
00124 float getResetTimef()
00125 {
00126 #ifdef Darwin
00127 const uint64_t now = mach_absolute_time();
00128 const int64_t elapsed = now - _start;
00129 _start = now;
00130 return ( elapsed * _timebaseInfo.numer / _timebaseInfo.denom /
00131 1000000.f );
00132 #elif defined (WIN32)
00133 LARGE_INTEGER now;
00134 QueryPerformanceCounter( &now );
00135 const float time = 1000.0f * (now.QuadPart - _start.QuadPart) /
00136 _frequency.QuadPart;
00137 _start = now;
00138 return time;
00139 #else
00140 struct timespec now;
00141 clock_gettime( CLOCK_REALTIME, &now );
00142 const float time = ( 1000.0f * (now.tv_sec - _start.tv_sec) +
00143 0.000001f * (now.tv_nsec - _start.tv_nsec));
00144 _start = now;
00145 return time;
00146 #endif
00147 }
00148
00153 int64_t getTime64() const
00154 {
00155 #ifdef Darwin
00156 const int64_t elapsed = mach_absolute_time() - _start;
00157 return ( elapsed * _timebaseInfo.numer / _timebaseInfo.denom /
00158 1000000 );
00159 #elif defined (WIN32)
00160 LARGE_INTEGER now;
00161 QueryPerformanceCounter( &now );
00162 return 1000 * (now.QuadPart - _start.QuadPart) /
00163 _frequency.QuadPart;
00164 #else
00165 struct timespec now;
00166 clock_gettime( CLOCK_REALTIME, &now );
00167 return ( 1000 * (now.tv_sec - _start.tv_sec) +
00168 static_cast< int64_t >(0.000001f *
00169 (now.tv_nsec-_start.tv_nsec)));
00170 #endif
00171 }
00172
00177 double getTimed() const
00178 {
00179 #ifdef Darwin
00180 const int64_t elapsed = mach_absolute_time() - _start;
00181 return ( elapsed * _timebaseInfo.numer / _timebaseInfo.denom /
00182 1000000. );
00183 #elif defined (WIN32)
00184 LARGE_INTEGER now;
00185 QueryPerformanceCounter( &now );
00186 return 1000.0 * (now.QuadPart - _start.QuadPart) /
00187 _frequency.QuadPart;
00188 #else
00189 struct timespec now;
00190 clock_gettime( CLOCK_REALTIME, &now );
00191 return ( 1000.0 * (now.tv_sec - _start.tv_sec) +
00192 0.000001 * (now.tv_nsec - _start.tv_nsec));
00193 #endif
00194 }
00195
00205 float getMilliSecondsf() const
00206 {
00207 #if defined (Darwin) || defined (WIN32)
00208 double time = getTimed();
00209 return static_cast<float>
00210 (time - static_cast<unsigned>(time/1000.) * 1000);
00211 #else
00212 struct timespec now;
00213 clock_gettime( CLOCK_REALTIME, &now );
00214
00215 if( now.tv_nsec < _start.tv_nsec )
00216 return ( 1000.f + 0.000001f*(now.tv_nsec - _start.tv_nsec));
00217
00218 return ( 0.000001f * ( now.tv_nsec - _start.tv_nsec ));
00219 #endif
00220 }
00221
00222 private:
00223 #ifdef Darwin
00224 uint64_t _start;
00225 mach_timebase_info_data_t _timebaseInfo;
00226 #elif defined (WIN32)
00227 LARGE_INTEGER _start;
00228 LARGE_INTEGER _frequency;
00229 #else
00230 struct timespec _start;
00231 #endif
00232 };
00233 }
00234 }
00235 #endif // EQBASE_CLOCK_H