timer.h
Go to the documentation of this file.
00001 // from: http://snipplr.com/view/40650/timer-class-for-both-unixlinuxmac-and-windows-system/
00002 
00004 // How to Use ////
00006 
00007 //#include <iostream>
00008 //#include "timer.h"
00009 //using namespace std;
00010 
00011 //int main()
00012 //{
00013 //    Timer timer;
00014 // 
00015 //    // start timer
00016 //    timer.start();
00017 // 
00018 //    // do something
00019 //    ...
00020 // 
00021 //    // stop timer
00022 //    timer.stop();
00023 // 
00024 //    // print the elapsed time in millisec
00025 //    cout << timer.getElapsedTimeInMilliSec() << " ms.\n";
00026 // 
00027 //    return 0;
00028 //}
00029 
00030 
00032 // Timer.h
00033 // =======
00034 // High Resolution Timer.
00035 // This timer is able to measure the elapsed time with 1 micro-second accuracy
00036 // in both Windows, Linux and Unix system 
00037 //
00038 //  AUTHOR: Song Ho Ahn (song.ahn@gmail.com)
00039 // CREATED: 2003-01-13
00040 // UPDATED: 2006-01-13
00041 //
00042 // Copyright (c) 2003 Song Ho Ahn
00044 
00045 #ifndef TIMER_H_DEF
00046 #define TIMER_H_DEF
00047 
00048 #ifndef __LINUX__   // Windows system specific
00049 #include <windows.h>
00050 #else          // Unix based system specific
00051 #include <sys/time.h>
00052 #endif
00053 
00054 #include <stdlib.h>
00055 
00056 class Timer
00057 {
00058 public:
00059         // default constructor
00060         Timer()
00061         {
00062 #ifdef WIN32
00063                 QueryPerformanceFrequency(&frequency);
00064                 startCount.QuadPart = 0;
00065                 endCount.QuadPart = 0;
00066 #else
00067                 startCount.tv_sec = startCount.tv_usec = 0;
00068                 endCount.tv_sec = endCount.tv_usec = 0;
00069 #endif
00070 
00071                 stopped = 0;
00072                 startTimeInMicroSec = 0;
00073                 endTimeInMicroSec = 0;
00074         }
00075 
00076         // default destructor
00077         ~Timer()
00078         {
00079         }
00080 
00082         // start timer.
00083         // startCount will be set at this point.
00085         void start()
00086         {
00087                 stopped = 0; // reset stop flag
00088 #ifdef WIN32
00089                 QueryPerformanceCounter(&startCount);
00090 #else
00091                 gettimeofday(&startCount, NULL);
00092 #endif
00093         }
00094 
00096         // stop the timer.
00097         // endCount will be set at this point.
00099         void stop()
00100         {
00101                 stopped = 1; // set timer stopped flag
00102 
00103 #ifdef WIN32
00104                 QueryPerformanceCounter(&endCount);
00105 #else
00106                 gettimeofday(&endCount, NULL);
00107 #endif
00108         }
00109 
00111         // same as getElapsedTimeInSec()
00113         double getElapsedTime()
00114         {
00115                 return this->getElapsedTimeInSec();
00116         }
00117 
00119         // divide elapsedTimeInMicroSec by 1000000
00121         double getElapsedTimeInSec()
00122         {
00123                 return this->getElapsedTimeInMicroSec() * 0.000001;
00124         }
00125 
00127         // divide elapsedTimeInMicroSec by 1000
00129         double getElapsedTimeInMilliSec()
00130         {
00131                 return this->getElapsedTimeInMicroSec() * 0.001;
00132         }
00133 
00135         // compute elapsed time in micro-second resolution.
00136         // other getElapsedTime will call this first, then convert to correspond resolution.
00138         double getElapsedTimeInMicroSec()
00139         {
00140 #ifdef WIN32
00141                 if(!stopped)
00142                 QueryPerformanceCounter(&endCount);
00143 
00144                 startTimeInMicroSec = startCount.QuadPart * (1000000.0 / frequency.QuadPart);
00145                 endTimeInMicroSec = endCount.QuadPart * (1000000.0 / frequency.QuadPart);
00146 #else
00147                 if (!stopped)
00148                         gettimeofday(&endCount, NULL);
00149 
00150                 startTimeInMicroSec = (startCount.tv_sec * 1000000.0) + startCount.tv_usec;
00151                 endTimeInMicroSec = (endCount.tv_sec * 1000000.0) + endCount.tv_usec;
00152 #endif
00153 
00154                 return endTimeInMicroSec - startTimeInMicroSec;
00155         }
00156 
00157 protected:
00158 
00159 private:
00160         double startTimeInMicroSec; // starting time in micro-second
00161         double endTimeInMicroSec; // ending time in micro-second
00162         int stopped; // stop flag
00163 #ifdef WIN32
00164         LARGE_INTEGER frequency; // ticks per second
00165         LARGE_INTEGER startCount; //
00166         LARGE_INTEGER endCount; //
00167 #else
00168         timeval startCount; //
00169         timeval endCount; //
00170 #endif
00171 };
00172 
00173 #endif // TIMER_H_DEF


cob_people_detection
Author(s): Richard Bormann , Thomas Zwölfer
autogenerated on Fri Aug 28 2015 10:24:13