g2o
timeutil.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef G2O_TIMEUTIL_H
28 #define G2O_TIMEUTIL_H
29 
30 #ifdef _WINDOWS
31 #include <time.h>
32 #else
33 #include <sys/time.h>
34 #endif
35 
36 #include <string>
37 
38 #include "g2o_stuff_api.h"
39 
41 // @{
42 
47 #ifndef DO_EVERY_TS
50 #define DO_EVERY_TS(secs, currentTime, code) \
51 if (1) {\
52  static double s_lastDone_ = (currentTime); \
53  double s_now_ = (currentTime); \
54  if (s_lastDone_ > s_now_) \
55  s_lastDone_ = s_now_; \
56  if (s_now_ - s_lastDone_ > (secs)) { \
57  code; \
58  s_lastDone_ = s_now_; \
59  }\
60 } else \
61  (void)0
62 #endif
63 
65 #ifndef DO_EVERY
66 #define DO_EVERY(secs, code) DO_EVERY_TS(secs, g2o::get_time(), code)
67 #endif
68 
69 #ifndef MEASURE_TIME
70 #define MEASURE_TIME(text, code) \
71  if(1) { \
72  double _start_time_ = g2o::get_time(); \
73  code; \
74  fprintf(stderr, "%s took %f sec\n", text, g2o::get_time() - _start_time_); \
75  } else \
76  (void) 0
77 #endif
78 
79 namespace g2o {
80 
81 #ifdef _WINDOWS
82 typedef struct timeval {
83  long tv_sec;
84  long tv_usec;
85 } timeval;
86 G2O_STUFF_API int gettimeofday(struct timeval *tv, struct timezone *tz);
87 #endif
88 
92 inline double get_time()
93 {
94  struct timeval ts;
95  gettimeofday(&ts,0);
96  return ts.tv_sec + ts.tv_usec*1e-6;
97 }
98 
108 
116  public:
117  ScopeTime(const char* title);
118  ~ScopeTime();
119  private:
120  std::string _title;
121  double _startTime;
122 };
123 
124 } // end namespace
125 
126 #ifndef MEASURE_FUNCTION_TIME
127 #define MEASURE_FUNCTION_TIME \
128  g2o::ScopeTime scopeTime(__PRETTY_FUNCTION__)
129 #endif
130 
131 
132 // @}
133 #endif
double get_monotonic_time()
Definition: timeutil.cpp:113
#define G2O_STUFF_API
Definition: g2o_stuff_api.h:30
double _startTime
Definition: timeutil.h:121
Class to measure the time spent in a scope.
Definition: timeutil.h:115
std::string _title
Definition: timeutil.h:120
double get_time()
Definition: timeutil.h:92