Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef PANGOLIN_TIMER_H
00029 #define PANGOLIN_TIMER_H
00030
00031 #include "pangolin.h"
00032
00033 #ifdef _UNIX_
00034 #include <sys/time.h>
00035 #endif
00036
00037 #ifdef _WIN_
00038 #include <windows.h>
00039 #endif
00040
00041 namespace pangolin
00042 {
00043
00044 #ifdef _UNIX_
00045 typedef timeval basetime;
00046
00047 inline basetime TimeNow()
00048 {
00049 basetime t;
00050 gettimeofday(&t,NULL);
00051 return t;
00052 }
00053
00054 inline basetime TimeFromSeconds(double seconds)
00055 {
00056 basetime t;
00057 t.tv_sec = (time_t)seconds;
00058 t.tv_usec = (useconds_t)((seconds - t.tv_sec) * 1E6);
00059 return t;
00060 }
00061
00062 inline basetime TimeAdd(basetime t1, basetime t2)
00063 {
00064 basetime t;
00065 t.tv_sec = t1.tv_sec + t2.tv_sec;
00066 t.tv_usec = t1.tv_usec + t2.tv_usec;
00067 if(t.tv_usec >= 1E6 )
00068 {
00069 t.tv_usec -= 1E6;
00070 t.tv_sec += 1;
00071 }
00072
00073 return t;
00074 }
00075
00076 inline double TimeDiff_s(basetime start, basetime end)
00077 {
00078 return (double)(end.tv_sec - start.tv_sec) + (double)(end.tv_usec - start.tv_usec) * 1E-6;
00079 }
00080 #endif
00081
00082 #ifdef _WIN_
00083 typedef LARGE_INTEGER basetime;
00084
00085 inline basetime TimeNow()
00086 {
00087 basetime t;
00088 QueryPerformanceCounter(&t);
00089 return t;
00090 }
00091
00092 inline double TimeDiff_s(basetime start, basetime end)
00093 {
00094 LARGE_INTEGER f;
00095 QueryPerformanceFrequency(&f);
00096 return (end.QuadPart - start.QuadPart) / f.QuadPart;
00097 }
00098
00099 inline basetime TimeFromSeconds(double seconds)
00100 {
00101 LARGE_INTEGER f;
00102 QueryPerformanceFrequency(&f);
00103 basetime t;
00104 t.QuadPart = (seconds * f);
00105 return t;
00106 }
00107
00108 inline basetime TimeAdd(basetime t1, basetime t2)
00109 {
00110 basetime t;
00111 t.QuadPart t1.QuadPart + t2.QuadPart;
00112 return t;
00113 }
00114 #endif
00115
00116 inline basetime WaitUntil(basetime t)
00117 {
00118
00119 basetime currtime = TimeNow();
00120 while( TimeDiff_s(currtime,t) > 0 )
00121 currtime = TimeNow();
00122 return currtime;
00123 }
00124
00125 struct Timer
00126 {
00127 Timer()
00128 {
00129 Reset();
00130 }
00131
00132 void Reset()
00133 {
00134 start = TimeNow();
00135 }
00136
00137 double Elapsed_s()
00138 {
00139 basetime currtime = TimeNow();
00140 return TimeDiff_s(start,currtime);
00141 }
00142
00143 basetime start;
00144 };
00145
00146 }
00147
00148 #endif //PANGOLIN_TIMER_H