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