Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef TIMEUTIL_H
00018 #define TIMEUTIL_H
00019
00020 #ifdef _WINDOWS
00021 #include <time.h>
00022 #else
00023 #include <sys/time.h>
00024 #endif
00025
00026 #include <string>
00027
00029
00030
00035
00036
00037 #ifndef DO_EVERY_TS
00038 #define DO_EVERY_TS(secs, currentTime, code) \
00039 if (1) {\
00040 static double s_lastDone_ = (currentTime); \
00041 double s_now_ = (currentTime); \
00042 if (s_lastDone_ > s_now_) \
00043 s_lastDone_ = s_now_; \
00044 if (s_now_ - s_lastDone_ > (secs)) { \
00045 code; \
00046 s_lastDone_ = s_now_; \
00047 }\
00048 } else \
00049 (void)0
00050 #endif
00051
00053 #ifndef DO_EVERY
00054 #define DO_EVERY(secs, code) DO_EVERY_TS(secs, g2o::get_time(), code)
00055 #endif
00056
00057 #ifndef MEASURE_TIME
00058 #define MEASURE_TIME(text, code) \
00059 if(1) { \
00060 double _start_time_ = g2o::get_time(); \
00061 code; \
00062 fprintf(stderr, "%s took %f sec\n", text, g2o::get_time() - _start_time_); \
00063 } else \
00064 (void) 0
00065 #endif
00066
00067 namespace g2o {
00068
00069 #ifdef _WINDOWS
00070 typedef struct timeval {
00071 long tv_sec;
00072 long tv_usec;
00073 } timeval;
00074 int gettimeofday(struct timeval *tv, struct timezone *tz);
00075 #endif
00076
00080 inline double get_time()
00081 {
00082 struct timeval ts;
00083 gettimeofday(&ts,0);
00084 return ts.tv_sec + ts.tv_usec*1e-6;
00085 }
00086
00093 class ScopeTime {
00094 public:
00095 ScopeTime(const char* title);
00096 ~ScopeTime();
00097 private:
00098 std::string _title;
00099 double _startTime;
00100 };
00101
00102 }
00103
00104 #define MEASURE_FUNCTION_TIME \
00105 g2o::ScopeTime scopeTime(__PRETTY_FUNCTION__)
00106
00107
00108
00109 #endif