00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _plTimer_h_
00019 #define _plTimer_h_
00020
00021
00022 #define PL_USE_SYSTIME
00023
00024 #if defined(WIN32) || defined(_WIN32)
00025 #undef PL_USE_SYSTIME
00026 #endif
00027
00028 #ifdef PL_USE_SYSTIME
00029 #include <sys/time.h>
00030 #else
00031 #include <time.h>
00032 #endif
00033
00034 #include <stdlib.h>
00035
00036 class plTimer
00037 {
00038 protected:
00039 double tstart;
00040 double tstop;
00041 #ifdef PL_USE_SYSTIME
00042 struct timeval tv;
00043 #endif
00044 bool activated;
00045 public:
00046 inline plTimer();
00047 inline void start();
00048 inline void stop();
00049 inline double get_time_sec() const;
00050 inline double get_time_msec() const;
00051 inline void activate_for_time_sec(double time_in_sec);
00052 inline void activate_for_time_msec(double time_in_sec);
00053 inline bool isActivated() const;
00054 inline bool expired() const;
00055 inline void reset();
00056
00057 static plTimer timer;
00058
00059 };
00060
00061
00062
00063 inline plTimer::plTimer()
00064 :tstart(0.0), tstop(0.0), activated(false)
00065 {}
00066
00067
00068 inline void plTimer::reset()
00069 {
00070 tstart = tstop = 0.0;
00071 activated = false;
00072 }
00073
00074
00075
00076 inline bool plTimer::isActivated() const
00077 {
00078 return activated;
00079 }
00080
00081
00082 #ifdef PL_USE_SYSTIME
00083
00084
00085 inline void plTimer::start()
00086 {
00087 gettimeofday(&tv,NULL);
00088 tstart = tv.tv_sec + tv.tv_usec*1e-6;
00089 }
00090
00091
00092 inline void plTimer::stop()
00093 {
00094 gettimeofday(&tv,NULL);
00095 tstop = tv.tv_sec + tv.tv_usec*1e-6;
00096 }
00097
00098
00099 inline double plTimer::get_time_sec() const
00100 {
00101 return tstop - tstart;
00102 }
00103
00104
00105 inline double plTimer::get_time_msec() const
00106 {
00107 return (tstop - tstart)*1000.0;
00108 }
00109
00110
00111 inline void plTimer::activate_for_time_sec(double time_in_sec)
00112 {
00113 gettimeofday(&tv,NULL);
00114 tstart = tv.tv_sec + tv.tv_usec*1e-6;
00115
00116 tstop = tstart + time_in_sec;
00117 activated = true;
00118 }
00119
00120
00121 inline void plTimer::activate_for_time_msec(double time_in_msec)
00122 {
00123 gettimeofday(&tv,NULL);
00124 tstart = tv.tv_sec + tv.tv_usec*1e-6;
00125
00126 tstop = tstart + time_in_msec*1e-3;
00127 activated = true;
00128 }
00129
00130
00131 inline bool plTimer::expired() const
00132 {
00133 if(!activated) return false;
00134
00135 struct timeval ltv;
00136 gettimeofday(<v,NULL);
00137 double current_time = ltv.tv_sec + ltv.tv_usec*1e-6;
00138
00139 return (current_time >= tstop);
00140 }
00141
00142
00143 #else
00144
00145 inline void plTimer::start()
00146 {
00147 tstart = clock();
00148 }
00149
00150
00151 inline void plTimer::stop()
00152 {
00153 tstop = clock();
00154 }
00155
00156
00157 inline double plTimer::get_time_sec() const
00158 {
00159 return (tstop - tstart)/CLOCKS_PER_SEC;
00160 }
00161
00162
00163 inline double plTimer::get_time_msec() const
00164 {
00165 return (tstop -tstart)*1000.0/CLOCKS_PER_SEC;
00166 }
00167
00168
00169
00170 inline void plTimer::activate_for_time_sec(double time_in_sec)
00171 {
00172 tstart = clock();
00173 tstop = tstart + time_in_sec*CLOCKS_PER_SEC;
00174 activated = true;
00175 }
00176
00177
00178 inline void plTimer::activate_for_time_msec(double time_in_msec)
00179 {
00180 tstart = clock();
00181 tstop = tstart + time_in_msec*1e-3*CLOCKS_PER_SEC;
00182 activated = true;
00183 }
00184
00185
00186 inline bool plTimer::expired() const
00187 {
00188 if(!activated) return false;
00189
00190 return (clock() >= tstop);
00191 }
00192
00193 #endif
00194
00195 #endif