00001 00059 /* 00060 * time.h 00061 * 00062 * Created on: Nov 21, 2011 00063 * Author: now me Joshua Hampp 00064 */ 00065 00066 #ifndef TIME_H_ 00067 #define TIME_H_ 00068 00069 namespace measurement_tools { 00070 00071 // Linux version of precision host timer. See http://www.informit.com/articles/article.aspx?p=23618&seqNum=8 00075 class PrecisionStopWatchAll { 00076 struct timeval precisionClock; 00077 00078 public: 00079 PrecisionStopWatchAll() { 00080 gettimeofday(&precisionClock, NULL); 00081 }; 00082 00083 void precisionStart() { 00084 gettimeofday(&precisionClock, NULL); 00085 }; 00086 00087 double precisionStop() { 00088 struct timeval precisionClockEnd; 00089 gettimeofday(&precisionClockEnd, NULL); 00090 return ((double)precisionClockEnd.tv_sec + 1.0e-6 * (double)precisionClockEnd.tv_usec) - ((double)precisionClock.tv_sec + 1.0e-6 * (double)precisionClock.tv_usec); 00091 }; 00092 }; 00093 00097 class PrecisionStopWatchThread { 00098 timespec precisionClock; 00099 00100 timespec diff(timespec start, timespec end) 00101 { 00102 timespec temp; 00103 if ((end.tv_nsec-start.tv_nsec)<0) { 00104 temp.tv_sec = end.tv_sec-start.tv_sec-1; 00105 temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec; 00106 } else { 00107 temp.tv_sec = end.tv_sec-start.tv_sec; 00108 temp.tv_nsec = end.tv_nsec-start.tv_nsec; 00109 } 00110 return temp; 00111 } 00112 00113 public: 00114 PrecisionStopWatchThread() { 00115 precisionStart(); 00116 }; 00117 00118 void precisionStart() { 00119 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &precisionClock); 00120 }; 00121 00122 double precisionStop() { 00123 timespec t; 00124 clock_gettime(CLOCK_THREAD_CPUTIME_ID, &t); 00125 t=diff(precisionClock,t); 00126 precisionStart(); 00127 return t.tv_sec+t.tv_nsec/1000000000.; 00128 }; 00129 }; 00130 } 00131 00132 #endif /* TIME_H_ */