Point Cloud Library (PCL)  1.9.1
time.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  * Copyright (c) 2012-, Open Perception, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  */
38 
39 #ifndef PCL_TIME_H_
40 #define PCL_TIME_H_
41 
42 #ifdef __GNUC__
43 #pragma GCC system_header
44 #endif
45 
46 #include <cmath>
47 #include <queue>
48 #include <string>
49 #ifndef Q_MOC_RUN
50 #include <boost/date_time/posix_time/posix_time.hpp>
51 #endif
52 
53 /**
54  * \file pcl/common/time.h
55  * Define methods for measuring time spent in code blocks
56  * \ingroup common
57  */
58 
59 /*@{*/
60 namespace pcl
61 {
62  /** \brief Simple stopwatch.
63  * \ingroup common
64  */
65  class StopWatch
66  {
67  public:
68  /** \brief Constructor. */
69  StopWatch () : start_time_ (boost::posix_time::microsec_clock::local_time ())
70  {
71  }
72 
73  /** \brief Destructor. */
74  virtual ~StopWatch () {}
75 
76  /** \brief Retrieve the time in milliseconds spent since the last call to \a reset(). */
77  inline double
79  {
80  boost::posix_time::ptime end_time = boost::posix_time::microsec_clock::local_time ();
81  return (static_cast<double> (((end_time - start_time_).total_milliseconds ())));
82  }
83 
84  /** \brief Retrieve the time in seconds spent since the last call to \a reset(). */
85  inline double
87  {
88  return (getTime () * 0.001f);
89  }
90 
91  /** \brief Reset the stopwatch to 0. */
92  inline void
93  reset ()
94  {
95  start_time_ = boost::posix_time::microsec_clock::local_time ();
96  }
97 
98  protected:
99  boost::posix_time::ptime start_time_;
100  };
101 
102  /** \brief Class to measure the time spent in a scope
103  *
104  * To use this class, e.g. to measure the time spent in a function,
105  * just create an instance at the beginning of the function. Example:
106  *
107  * \code
108  * {
109  * pcl::ScopeTime t1 ("calculation");
110  *
111  * // ... perform calculation here
112  * }
113  * \endcode
114  *
115  * \ingroup common
116  */
117  class ScopeTime : public StopWatch
118  {
119  public:
120  inline ScopeTime (const char* title) :
121  title_ (std::string (title))
122  {
123  start_time_ = boost::posix_time::microsec_clock::local_time ();
124  }
125 
126  inline ScopeTime () :
127  title_ (std::string (""))
128  {
129  start_time_ = boost::posix_time::microsec_clock::local_time ();
130  }
131 
132  inline ~ScopeTime ()
133  {
134  double val = this->getTime ();
135  std::cerr << title_ << " took " << val << "ms.\n";
136  }
137 
138  private:
139  std::string title_;
140  };
141 
142  /** \brief A helper class to measure frequency of a certain event.
143  *
144  * To use this class create an instance and call event() function every time
145  * the event in question occurs. The estimated frequency can be retrieved
146  * with getFrequency() function.
147  *
148  * \author Sergey Alexandrov
149  * \ingroup common
150  */
152  {
153 
154  public:
155 
156  /** \brief Constructor.
157  *
158  * \param[in] window_size number of most recent events that are
159  * considered in frequency estimation (default: 30) */
160  EventFrequency (size_t window_size = 30)
161  : window_size_ (window_size)
162  {
163  stop_watch_.reset ();
164  }
165 
166  /** \brief Notifies the class that the event occurred. */
167  void event ()
168  {
169  event_time_queue_.push (stop_watch_.getTimeSeconds ());
170  if (event_time_queue_.size () > window_size_)
171  event_time_queue_.pop ();
172  }
173 
174  /** \brief Retrieve the estimated frequency. */
175  double
176  getFrequency () const
177  {
178  if (event_time_queue_.size () < 2)
179  return (0.0);
180  return ((event_time_queue_.size () - 1) /
181  (event_time_queue_.back () - event_time_queue_.front ()));
182  }
183 
184  /** \brief Reset frequency computation. */
185  void reset ()
186  {
187  stop_watch_.reset ();
188  event_time_queue_ = std::queue<double> ();
189  }
190 
191  private:
192 
193  pcl::StopWatch stop_watch_;
194  std::queue<double> event_time_queue_;
195  const size_t window_size_;
196 
197  };
198 
199 #ifndef MEASURE_FUNCTION_TIME
200 #define MEASURE_FUNCTION_TIME \
201  ScopeTime scopeTime(__func__)
202 #endif
203 
204 inline double
206 {
207  boost::posix_time::ptime epoch_time (boost::gregorian::date (1970, 1, 1));
208  boost::posix_time::ptime current_time = boost::posix_time::microsec_clock::local_time ();
209  return (static_cast<double>((current_time - epoch_time).total_nanoseconds ()) * 1.0e-9);
210 }
211 
212 /// Executes code, only if secs are gone since last exec.
213 #ifndef DO_EVERY_TS
214 #define DO_EVERY_TS(secs, currentTime, code) \
215 if (1) {\
216  static double s_lastDone_ = 0.0; \
217  double s_now_ = (currentTime); \
218  if (s_lastDone_ > s_now_) \
219  s_lastDone_ = s_now_; \
220  if ((s_now_ - s_lastDone_) > (secs)) { \
221  code; \
222  s_lastDone_ = s_now_; \
223  }\
224 } else \
225  (void)0
226 #endif
227 
228 /// Executes code, only if secs are gone since last exec.
229 #ifndef DO_EVERY
230 #define DO_EVERY(secs, code) \
231  DO_EVERY_TS(secs, pcl::getTime(), code)
232 #endif
233 
234 } // end namespace
235 /*@}*/
236 
237 #endif //#ifndef PCL_NORMS_H_
void event()
Notifies the class that the event occurred.
Definition: time.h:167
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
virtual ~StopWatch()
Destructor.
Definition: time.h:74
Class to measure the time spent in a scope.
Definition: time.h:117
ScopeTime(const char *title)
Definition: time.h:120
A helper class to measure frequency of a certain event.
Definition: time.h:151
double getFrequency() const
Retrieve the estimated frequency.
Definition: time.h:176
EventFrequency(size_t window_size=30)
Constructor.
Definition: time.h:160
void reset()
Reset frequency computation.
Definition: time.h:185
boost::posix_time::ptime start_time_
Definition: time.h:99
void reset()
Reset the stopwatch to 0.
Definition: time.h:93
double getTimeSeconds()
Retrieve the time in seconds spent since the last call to reset().
Definition: time.h:86
Simple stopwatch.
Definition: time.h:65
StopWatch()
Constructor.
Definition: time.h:69
double getTime()
Retrieve the time in milliseconds spent since the last call to reset().
Definition: time.h:78
~ScopeTime()
Definition: time.h:132