Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _CHRONOMETER_H_
00011 #define _CHRONOMETER_H_
00012
00013 #ifdef QT_CORE_LIB
00014
00015 #include <QTime>
00016
00017 namespace mt{
00018 typedef QTime Clock;
00019 }
00020
00021 #else
00022
00023 #ifdef _WIN32
00024 #include <windows.h>
00025 #else
00026 #include <sys/time.h>
00027 #endif
00028
00029 namespace mt{
00030
00034 class Clock{
00035
00036 public:
00038 Clock()
00039 {
00040 #ifdef _WIN32
00041 QueryPerformanceFrequency(&freq);
00042 #endif
00043 }
00044
00046 static Clock currentTime()
00047 {
00048 Clock c;
00049 c.start();
00050 return c;
00051 }
00052
00054 inline void start()
00055 {
00056 #ifdef _WIN32
00057 QueryPerformanceCounter(&tini);
00058 #else
00059 gettimeofday (&tini, 0);
00060 #endif
00061 }
00062
00066 inline int restart()
00067 {
00068 return elapsedAux(true);
00069 }
00070
00073 inline int elapsed()
00074 {
00075 return elapsedAux(false);
00076 }
00077
00078
00079
00080
00081 int msecsTo ( const Clock & t ) const
00082 {
00083 #ifdef _WIN32
00084 LARGE_INTEGER tacum;
00085 tacum.QuadPart = (t.tini.QuadPart - this->tini.QuadPart);
00086 return (int) tacum.QuadPart / ((int)freq.QuadPart / 1000);
00087 #else
00088 long int sec = t.tini.tv_sec - this->tini.tv_sec;
00089 long int usec = t.tini.tv_usec - this->tini.tv_usec;
00090 if (usec < 0) {
00091 sec --;
00092 usec += 1000000u;
00093 }
00094 return (int) sec * 1000.0f + (int) usec / 1000.0f;
00095 #endif
00096 }
00097
00098 protected:
00099
00100 #ifdef _WIN32
00101
00102 LARGE_INTEGER tini;
00103
00104 LARGE_INTEGER freq;
00105 #else
00106
00107 timeval tini;
00108 #endif
00109
00113 int elapsedAux( bool restart )
00114 {
00115 #ifdef _WIN32
00116 LARGE_INTEGER tnow;
00117 LARGE_INTEGER tacum;
00118
00119 QueryPerformanceCounter(&tnow);
00120 tacum.QuadPart = (tnow.QuadPart - tini.QuadPart);
00121
00122 if(restart)
00123 tini.QuadPart = tnow.QuadPart;
00124
00125 return (int) tacum.QuadPart / ((int)freq.QuadPart / 1000);
00126
00127 #else
00128 timeval tnow;
00129 gettimeofday (&tnow, 0);
00130
00131 long int sec = tnow.tv_sec - tini.tv_sec;
00132 long int usec = tnow.tv_usec - tini.tv_usec;
00133 if (usec < 0) {
00134 sec --;
00135 usec += 1000000u;
00136 }
00137 if(restart)
00138 tini = tnow;
00139
00140 return (int) sec * 1000.0f + (int) usec / 1000.0f;
00141
00142 #endif
00143 }
00144 };
00145
00146 }
00147
00148 #endif //ifdef QT_CORE_LIB
00149 #endif //ifndef _CHRONOMETER_H_