Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

Timer.h

Go to the documentation of this file.
00001 #ifndef Impala_Basis_Timer_h
00002 #define Impala_Basis_Timer_h
00003 
00004 #ifdef unix
00005 // mode = 0
00006 #include <sys/param.h>
00007 #include <sys/times.h>
00008 #include <limits.h>
00009 #include <unistd.h>
00010 // mode = 1
00011 #include <sys/time.h>
00012 #else
00013 // mode = 0
00014 #include <ctime>
00015 // mode = 1
00016 #include <windows.h> // it might be possible to include a 'smaller' headerfile
00017 #undef small // the fucking idiots define this to char apparently
00018 #endif
00019 
00020 #include <stdio.h>
00021 #include "Basis/String.h"
00022 
00023 namespace Impala
00024 {
00025 
00026 
00027 class Timer
00028 {
00029 public: 
00030 
00031     // mode 0 == process time using milliseconds
00032     // mode 1 == real time using high resolution
00033     Timer(int mode = 1) : mMode(mode) // also starts the timer
00034     {
00035 #ifdef unix
00036         // no platform dependent initialization required
00037 #else
00038         LARGE_INTEGER freqStruct;
00039         if (QueryPerformanceFrequency(&freqStruct))
00040         {
00041             mFreq = freqStruct.HighPart;
00042             mFreq = mFreq << 32;
00043             mFreq |= freqStruct.LowPart;
00044         }
00045         else
00046         {
00047             std::cout << "Timer: no QueryPerformanceFrequency" << std::endl;
00048         }
00049         if (! QueryPerformanceCounter(&freqStruct)) // is this test needed?
00050             std::cout << "Timer: no QueryPerformanceCounter" << std::endl;
00051 #endif
00052         Start();
00053     }
00054 
00055     void
00056     Start() // (re-) start the timer
00057     {
00058 #ifdef unix
00059         if (mMode == 0)
00060         {
00061             times(&mCpuTime);
00062             mUsrTime = mCpuTime.tms_utime;
00063             mSysTime = mCpuTime.tms_stime;
00064         }
00065         else
00066         {
00067             gettimeofday(&mStartTime, 0);
00068         }
00069 #else
00070         if (mMode == 0)
00071         {
00072             mCpuTime = clock();
00073         }
00074         else
00075         {
00076             LARGE_INTEGER countStruct;
00077             QueryPerformanceCounter(&countStruct);
00078             mStartTime = countStruct.HighPart;
00079             mStartTime = mStartTime << 32;
00080             mStartTime |= countStruct.LowPart;
00081         }
00082 #endif
00083     }
00084 
00085     double
00086     SplitTime() // returns elapsed time (seconds), timer keeps running
00087     {
00088         double val;
00089 #ifdef unix
00090         if (mMode == 0)
00091         {
00092             times(&mCpuTime);
00093             time_t splitUsrTime = mCpuTime.tms_utime - mUsrTime;
00094             time_t splitSysTime = mCpuTime.tms_stime - mSysTime;
00095             val = (double) (splitUsrTime + splitSysTime) / HZ;
00096         }
00097         else
00098         {
00099             struct timeval splitTime;
00100             gettimeofday(&splitTime, 0);
00101             val = (double) (splitTime.tv_sec - mStartTime.tv_sec);
00102             long uSec = splitTime.tv_usec - mStartTime.tv_usec;
00103             val += (double) uSec / 1000000;
00104         }
00105 #else
00106         if (mMode == 0)
00107         {
00108             clock_t splitCpuTime = clock() - mCpuTime;
00109             val = (double) splitCpuTime / CLOCKS_PER_SEC;
00110         }
00111         else
00112         {
00113             LARGE_INTEGER splitTimeStruct;
00114             QueryPerformanceCounter(&splitTimeStruct);
00115             LONGLONG splitTime = splitTimeStruct.HighPart;
00116             splitTime = splitTime << 32;
00117             splitTime |= splitTimeStruct.LowPart;
00118             splitTime -= mStartTime;
00119             val = (double) splitTime / mFreq;
00120         }
00121 #endif
00122         return val;
00123     }
00124 
00125     String
00126     SplitTimeStr(double divide = 1.0) // SplitTime/divide as a formatted string
00127     {
00128         char printBuf[2048];
00129         double secs = SplitTime() / divide;
00130         int hours = int (secs / 3600);
00131         secs -= hours * 3600;
00132         int minutes = int (secs / 60);
00133         secs -= minutes * 60;
00134         sprintf(printBuf, "%dh%dm%.2fs", hours, minutes, secs);
00135         return String(printBuf);
00136     }
00137 
00138     double
00139     Stop() // stop timer and return elapsed time
00140     {
00141         double val;
00142 #ifdef unix
00143         if (mMode == 0)
00144         {
00145             times(&mCpuTime);
00146             mUsrTime = mCpuTime.tms_utime - mUsrTime;
00147             mSysTime = mCpuTime.tms_stime - mSysTime;
00148             val = (double) (mUsrTime + mSysTime) / HZ;
00149         }
00150         else
00151         {
00152             struct timeval splitTime;
00153             gettimeofday(&splitTime, 0);
00154             val = (double) (splitTime.tv_sec - mStartTime.tv_sec);
00155             long uSec = splitTime.tv_usec - mStartTime.tv_usec;
00156             val += (double) uSec / 1000000;
00157         }
00158 #else
00159         if (mMode == 0)
00160         {
00161             mCpuTime = clock() - mCpuTime;
00162             val = (double) mCpuTime / CLOCKS_PER_SEC;
00163         }
00164         else
00165         {
00166             LARGE_INTEGER splitTimeStruct;
00167             QueryPerformanceCounter(&splitTimeStruct);
00168             LONGLONG splitTime = splitTimeStruct.HighPart;
00169             splitTime = splitTime << 32;
00170             splitTime |= splitTimeStruct.LowPart;
00171             splitTime -= mStartTime;
00172             val = (double) splitTime / mFreq;
00173         }
00174 #endif
00175         return val;
00176     }
00177 
00178 private:
00179 
00180     int            mMode;
00181 
00182 #ifdef unix
00183     // mode = 0
00184     struct tms     mCpuTime;
00185     time_t         mSysTime;
00186     time_t         mUsrTime;
00187     // mode = 1
00188     struct timeval mStartTime;
00189 #else
00190     // mode = 0
00191     clock_t        mCpuTime;
00192     // mode = 1
00193     LONGLONG       mFreq; // signed 64bit integer
00194     LONGLONG       mStartTime;
00195 #endif
00196 
00197 };
00198 
00199 } // namespace Impala
00200 
00201 #endif

Generated on Fri Mar 19 09:30:43 2010 for ImpalaSrc by  doxygen 1.5.1