Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a helper class EventFrequency #850

Merged
merged 1 commit into from
Sep 11, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions common/include/pcl/common/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#endif

#include <cmath>
#include <queue>
#include <string>
#include <boost/date_time/posix_time/posix_time.hpp>

Expand Down Expand Up @@ -136,6 +137,62 @@ namespace pcl
std::string title_;
};

/** \brief A helper class to measure frequency of a certain event.
*
* To use this class create an instance and call event() function every time
* the event in question occurs. The estimated frequency can be retrieved
* with getFrequency() function.
*
* \author Sergey Alexandrov
* \ingroup common
*/
class EventFrequency
{

public:

/** \brief Constructor.
*
* \param[i] window_size number of most recent events that are
* considered in frequency estimation (default: 30) */
EventFrequency (size_t window_size = 30)
: window_size_ (window_size)
{
stop_watch_.reset ();
}

/** \brief Notifies the class that the event occured. */
void event ()
{
event_time_queue_.push (stop_watch_.getTimeSeconds ());
if (event_time_queue_.size () > window_size_)
event_time_queue_.pop ();
}

/** \brief Retrieve the estimated frequency. */
double
getFrequency () const
{
if (event_time_queue_.size () < 2)
return (0.0);
return ((event_time_queue_.size () - 1) /
(event_time_queue_.back () - event_time_queue_.front ()));
}

/** \brief Reset frequency computation. */
void reset ()
{
stop_watch_.reset ();
event_time_queue_ = std::queue<double> ();
}

private:

pcl::StopWatch stop_watch_;
std::queue<double> event_time_queue_;
const size_t window_size_;

};

#ifndef MEASURE_FUNCTION_TIME
#define MEASURE_FUNCTION_TIME \
Expand Down