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