timeutil.h
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 // 
00004 // g2o is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU Lesser General Public License as published
00006 // by the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 // 
00009 // g2o is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU Lesser General Public License for more details.
00013 // 
00014 // You should have received a copy of the GNU Lesser General Public License
00015 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
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 } // end namespace
00103 
00104 #define MEASURE_FUNCTION_TIME \
00105   g2o::ScopeTime scopeTime(__PRETTY_FUNCTION__)
00106 
00107 
00108 // @}
00109 #endif


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:33:08