Point Cloud Library (PCL)  1.7.2
time.h
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 <string>
48 #ifndef Q_MOC_RUN
49 #include <boost/date_time/posix_time/posix_time.hpp>
50 #endif
51 
52 /**
53  * \file pcl/common/time.h
54  * Define methods for measuring time spent in code blocks
55  * \ingroup common
56  */
57 
58 /*@{*/
59 namespace pcl
60 {
61  /** \brief Simple stopwatch.
62  * \ingroup common
63  */
64  class StopWatch
65  {
66  public:
67  /** \brief Constructor. */
68  StopWatch () : start_time_ (boost::posix_time::microsec_clock::local_time ())
69  {
70  }
71 
72  /** \brief Destructor. */
73  virtual ~StopWatch () {}
74 
75  /** \brief Retrieve the time in milliseconds spent since the last call to \a reset(). */
76  inline double
78  {
79  boost::posix_time::ptime end_time = boost::posix_time::microsec_clock::local_time ();
80  return (static_cast<double> (((end_time - start_time_).total_milliseconds ())));
81  }
82 
83  /** \brief Retrieve the time in seconds spent since the last call to \a reset(). */
84  inline double
86  {
87  return (getTime () * 0.001f);
88  }
89 
90  /** \brief Reset the stopwatch to 0. */
91  inline void
92  reset ()
93  {
94  start_time_ = boost::posix_time::microsec_clock::local_time ();
95  }
96 
97  protected:
98  boost::posix_time::ptime start_time_;
99  };
100 
101  /** \brief Class to measure the time spent in a scope
102  *
103  * To use this class, e.g. to measure the time spent in a function,
104  * just create an instance at the beginning of the function. Example:
105  *
106  * \code
107  * {
108  * pcl::ScopeTime t1 ("calculation");
109  *
110  * // ... perform calculation here
111  * }
112  * \endcode
113  *
114  * \ingroup common
115  */
116  class ScopeTime : public StopWatch
117  {
118  public:
119  inline ScopeTime (const char* title) :
120  title_ (std::string (title))
121  {
122  start_time_ = boost::posix_time::microsec_clock::local_time ();
123  }
124 
125  inline ScopeTime () :
126  title_ (std::string (""))
127  {
128  start_time_ = boost::posix_time::microsec_clock::local_time ();
129  }
130 
131  inline ~ScopeTime ()
132  {
133  double val = this->getTime ();
134  std::cerr << title_ << " took " << val << "ms.\n";
135  }
136 
137  private:
138  std::string title_;
139  };
140 
141 
142 #ifndef MEASURE_FUNCTION_TIME
143 #define MEASURE_FUNCTION_TIME \
144  ScopeTime scopeTime(__func__)
145 #endif
146 
147 inline double
149 {
150  boost::posix_time::ptime epoch_time (boost::gregorian::date (1970, 1, 1));
151  boost::posix_time::ptime current_time = boost::posix_time::microsec_clock::local_time ();
152  return (static_cast<double>((current_time - epoch_time).total_nanoseconds ()) * 1.0e-9);
153 }
154 
155 /// Executes code, only if secs are gone since last exec.
156 #ifndef DO_EVERY_TS
157 #define DO_EVERY_TS(secs, currentTime, code) \
158 if (1) {\
159  static double s_lastDone_ = 0.0; \
160  double s_now_ = (currentTime); \
161  if (s_lastDone_ > s_now_) \
162  s_lastDone_ = s_now_; \
163  if ((s_now_ - s_lastDone_) > (secs)) { \
164  code; \
165  s_lastDone_ = s_now_; \
166  }\
167 } else \
168  (void)0
169 #endif
170 
171 /// Executes code, only if secs are gone since last exec.
172 #ifndef DO_EVERY
173 #define DO_EVERY(secs, code) \
174  DO_EVERY_TS(secs, pcl::getTime(), code)
175 #endif
176 
177 } // end namespace
178 /*@}*/
179 
180 #endif //#ifndef PCL_NORMS_H_
virtual ~StopWatch()
Destructor.
Definition: time.h:73
Class to measure the time spent in a scope.
Definition: time.h:116
ScopeTime(const char *title)
Definition: time.h:119
boost::posix_time::ptime start_time_
Definition: time.h:98
void reset()
Reset the stopwatch to 0.
Definition: time.h:92
double getTimeSeconds()
Retrieve the time in seconds spent since the last call to reset().
Definition: time.h:85
Simple stopwatch.
Definition: time.h:64
double getTime()
Definition: time.h:148
StopWatch()
Constructor.
Definition: time.h:68
double getTime()
Retrieve the time in milliseconds spent since the last call to reset().
Definition: time.h:77
~ScopeTime()
Definition: time.h:131