$search
00001 00060 #ifndef STOPWATCH_H_ 00061 #define STOPWATCH_H_ 00062 00063 00064 #include <ctime> 00065 #include <sstream> 00066 #ifdef _WIN32 00067 #include <Windows.h> 00068 #endif 00069 00070 00071 // low resolution stop watch 00072 class SimpleStopWatch { 00073 clock_t startClock; 00074 00075 public: 00076 // start time measurement at this moment 00077 void start() { startClock = clock(); }; 00078 00079 // returns elapsed time since last start() in seconds 00080 double stop() { return ((double)(clock()-startClock))/(double)CLOCKS_PER_SEC; }; 00081 }; 00082 00083 00084 // high performance stop watch 00085 #ifdef _WIN32 00086 class PrecisionStopWatch { 00087 LARGE_INTEGER precisionClock; 00088 LARGE_INTEGER precisionFrequency; 00089 00090 public: 00091 PrecisionStopWatch() { 00092 precisionClock.QuadPart = 0; 00093 if(!QueryPerformanceFrequency(&precisionFrequency)) printf("PrecisionStopWatch: Error: Frequency query failed.\n"); 00094 }; 00095 00096 // start time measurement at this moment 00097 void precisionStart() { 00098 if(!QueryPerformanceCounter(&precisionClock)) printf("PrecisionStopWatch: Error: precisionStart query failed.\n"); 00099 }; 00100 00101 // returns elapsed time since last precisionStart() in seconds 00102 double precisionStop() { 00103 LARGE_INTEGER precisionClockEnd; 00104 if(!QueryPerformanceCounter(&precisionClockEnd)) printf("PrecisionStopWatch: Error: precisionStop query failed.\n"); 00105 return (double)(precisionClockEnd.QuadPart - precisionClock.QuadPart) / (double)precisionFrequency.QuadPart; 00106 }; 00107 }; 00108 #else 00109 // Linux version of precision host timer. See http://www.informit.com/articles/article.aspx?p=23618&seqNum=8 00110 class PrecisionStopWatch { 00111 struct timeval precisionClock; 00112 00113 public: 00114 PrecisionStopWatch() { 00115 gettimeofday(&precisionClock, NULL); 00116 }; 00117 00118 void precisionStart() { 00119 gettimeofday(&precisionClock, NULL); 00120 }; 00121 00122 double precisionStop() { 00123 struct timeval precisionClockEnd; 00124 gettimeofday(&precisionClockEnd, NULL); 00125 return ((double)precisionClockEnd.tv_sec + 1.0e-6 * (double)precisionClockEnd.tv_usec) - ((double)precisionClock.tv_sec + 1.0e-6 * (double)precisionClock.tv_usec); 00126 }; 00127 }; 00128 #endif 00129 00130 #endif //STOPWATCH_H_