Point Cloud Library (PCL)  1.9.1-dev
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 #pragma once
40 
41 #include <chrono>
42 #include <iostream>
43 #include <queue>
44 #include <string>
45 
46 /**
47  * \file pcl/common/time.h
48  * Define methods for measuring time spent in code blocks
49  * \ingroup common
50  */
51 
52 /*@{*/
53 namespace pcl
54 {
55  /** \brief Simple stopwatch.
56  * \ingroup common
57  */
58  class StopWatch
59  {
60  public:
61  /** \brief Constructor. */
62  StopWatch () : start_time_ (std::chrono::steady_clock::now())
63  {
64  }
65 
66  /** \brief Destructor. */
67  virtual ~StopWatch () = default;
68 
69  /** \brief Retrieve the time in milliseconds spent since the last call to \a reset(). */
70  inline double
71  getTime () const
72  {
73  auto end_time = std::chrono::steady_clock::now();
74  return std::chrono::duration<double, std::ratio<1, 1000>>(end_time - start_time_).count();
75  }
76 
77  /** \brief Retrieve the time in seconds spent since the last call to \a reset(). */
78  inline double
79  getTimeSeconds () const
80  {
81  return (getTime () * 0.001);
82  }
83 
84  /** \brief Reset the stopwatch to 0. */
85  inline void
86  reset ()
87  {
88  start_time_ = std::chrono::steady_clock::now();
89  }
90 
91  protected:
92  std::chrono::time_point<std::chrono::steady_clock> start_time_;
93  };
94 
95  /** \brief Class to measure the time spent in a scope
96  *
97  * To use this class, e.g. to measure the time spent in a function,
98  * just create an instance at the beginning of the function. Example:
99  *
100  * \code
101  * {
102  * pcl::ScopeTime t1 ("calculation");
103  *
104  * // ... perform calculation here
105  * }
106  * \endcode
107  *
108  * \ingroup common
109  */
110  class ScopeTime : public StopWatch
111  {
112  public:
113  inline ScopeTime (const std::string &title = "") :
114  title_ (title)
115  {
116  }
117 
118  inline ~ScopeTime ()
119  {
120  double val = this->getTime ();
121  std::cerr << title_ << " took " << val << "ms.\n";
122  }
123 
124  private:
125  std::string title_;
126  };
127 
128  /** \brief A helper class to measure frequency of a certain event.
129  *
130  * To use this class create an instance and call event() function every time
131  * the event in question occurs. The estimated frequency can be retrieved
132  * with getFrequency() function.
133  *
134  * \author Sergey Alexandrov
135  * \ingroup common
136  */
138  {
139 
140  public:
141 
142  /** \brief Constructor.
143  *
144  * \param[in] window_size number of most recent events that are
145  * considered in frequency estimation (default: 30) */
146  EventFrequency (std::size_t window_size = 30)
147  : window_size_ (window_size)
148  {
149  stop_watch_.reset ();
150  }
151 
152  /** \brief Notifies the class that the event occurred. */
153  void event ()
154  {
155  event_time_queue_.push (stop_watch_.getTimeSeconds ());
156  if (event_time_queue_.size () > window_size_)
157  event_time_queue_.pop ();
158  }
159 
160  /** \brief Retrieve the estimated frequency. */
161  double
162  getFrequency () const
163  {
164  if (event_time_queue_.size () < 2)
165  return (0.0);
166  return ((event_time_queue_.size () - 1) /
167  (event_time_queue_.back () - event_time_queue_.front ()));
168  }
169 
170  /** \brief Reset frequency computation. */
171  void reset ()
172  {
173  stop_watch_.reset ();
174  event_time_queue_ = std::queue<double> ();
175  }
176 
177  private:
178 
179  pcl::StopWatch stop_watch_;
180  std::queue<double> event_time_queue_;
181  const std::size_t window_size_;
182 
183  };
184 
185 #ifndef MEASURE_FUNCTION_TIME
186 #define MEASURE_FUNCTION_TIME \
187  ScopeTime scopeTime(__func__)
188 #endif
189 
190 inline double
192 {
193  return std::chrono::duration<double>(std::chrono::system_clock::now().time_since_epoch()).count();
194 }
195 
196 /// Executes code, only if secs are gone since last exec.
197 #ifndef DO_EVERY_TS
198 #define DO_EVERY_TS(secs, currentTime, code) \
199 if (1) {\
200  static double s_lastDone_ = 0.0; \
201  double s_now_ = (currentTime); \
202  if (s_lastDone_ > s_now_) \
203  s_lastDone_ = s_now_; \
204  if ((s_now_ - s_lastDone_) > (secs)) { \
205  code; \
206  s_lastDone_ = s_now_; \
207  }\
208 } else \
209  (void)0
210 #endif
211 
212 /// Executes code, only if secs are gone since last exec.
213 #ifndef DO_EVERY
214 #define DO_EVERY(secs, code) \
215  DO_EVERY_TS(secs, pcl::getTime(), code)
216 #endif
217 
218 } // end namespace
219 /*@}*/
double getTimeSeconds() const
Retrieve the time in seconds spent since the last call to reset().
Definition: time.h:79
void event()
Notifies the class that the event occurred.
Definition: time.h:153
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
ScopeTime(const std::string &title="")
Definition: time.h:113
MovingLeastSquares represent an implementation of the MLS (Moving Least Squares) algorithm for data s...
Definition: mls.h:252
Class to measure the time spent in a scope.
Definition: time.h:110
std::chrono::time_point< std::chrono::steady_clock > start_time_
Definition: time.h:92
A helper class to measure frequency of a certain event.
Definition: time.h:137
double getTime() const
Retrieve the time in milliseconds spent since the last call to reset().
Definition: time.h:71
double getFrequency() const
Retrieve the estimated frequency.
Definition: time.h:162
virtual ~StopWatch()=default
Destructor.
void reset()
Reset frequency computation.
Definition: time.h:171
void reset()
Reset the stopwatch to 0.
Definition: time.h:86
Simple stopwatch.
Definition: time.h:58
EventFrequency(std::size_t window_size=30)
Constructor.
Definition: time.h:146
StopWatch()
Constructor.
Definition: time.h:62
~ScopeTime()
Definition: time.h:118