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