time.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010-2012, Willow Garage, Inc.
00006  *  Copyright (c) 2012-, Open Perception, Inc.
00007  *
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *
00014  *   * Redistributions of source code must retain the above copyright
00015  *     notice, this list of conditions and the following disclaimer.
00016  *   * Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions and the following
00018  *     disclaimer in the documentation and/or other materials provided
00019  *     with the distribution.
00020  *   * Neither the name of the copyright holder(s) nor the names of its
00021  *     contributors may be used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  *  POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  */
00038 
00039 #ifndef PCL_TIME_H_
00040 #define PCL_TIME_H_
00041 
00042 #ifdef __GNUC__
00043 #pragma GCC system_header 
00044 #endif
00045 
00046 #include <cmath>
00047 #include <string>
00048 #include <boost/date_time/posix_time/posix_time.hpp>
00049 
00057 namespace pcl
00058 {
00062   class StopWatch
00063   {
00064     public:
00066       StopWatch () : start_time_ (boost::posix_time::microsec_clock::local_time ())
00067       {
00068       }
00069 
00071       virtual ~StopWatch () {}
00072 
00074       inline double
00075       getTime ()
00076       {
00077         boost::posix_time::ptime end_time = boost::posix_time::microsec_clock::local_time ();
00078         return (static_cast<double> (((end_time - start_time_).total_milliseconds ())));
00079       }
00080 
00082       inline double
00083       getTimeSeconds ()
00084       {
00085         return (getTime () * 0.001f);
00086       }
00087 
00089       inline void
00090       reset ()
00091       {
00092         start_time_ = boost::posix_time::microsec_clock::local_time ();
00093       }
00094 
00095     protected:
00096       boost::posix_time::ptime start_time_;
00097   };
00098 
00114   class ScopeTime : public StopWatch
00115   {
00116     public:
00117       inline ScopeTime (const char* title) : 
00118         title_ (std::string (title))
00119       {
00120         start_time_ = boost::posix_time::microsec_clock::local_time ();
00121       }
00122 
00123       inline ScopeTime () :
00124         title_ (std::string (""))
00125       {
00126         start_time_ = boost::posix_time::microsec_clock::local_time ();
00127       }
00128 
00129       inline ~ScopeTime ()
00130       {
00131         double val = this->getTime ();
00132         std::cerr << title_ << " took " << val << "ms.\n";
00133       }
00134 
00135     private:
00136       std::string title_;
00137   };
00138 
00139 
00140 #ifndef MEASURE_FUNCTION_TIME
00141 #define MEASURE_FUNCTION_TIME \
00142   ScopeTime scopeTime(__func__)
00143 #endif
00144 
00145 inline double 
00146 getTime ()
00147 {
00148   boost::posix_time::ptime epoch_time (boost::gregorian::date (1970, 1, 1));
00149   boost::posix_time::ptime current_time = boost::posix_time::microsec_clock::local_time ();
00150   return (static_cast<double>((current_time - epoch_time).total_nanoseconds ()) * 1.0e-9);
00151 }
00152 
00154 #ifndef DO_EVERY_TS
00155 #define DO_EVERY_TS(secs, currentTime, code) \
00156 if (1) {\
00157   static double s_lastDone_ = 0.0; \
00158   double s_now_ = (currentTime); \
00159   if (s_lastDone_ > s_now_) \
00160     s_lastDone_ = s_now_; \
00161   if ((s_now_ - s_lastDone_) > (secs)) {        \
00162     code; \
00163     s_lastDone_ = s_now_; \
00164   }\
00165 } else \
00166   (void)0
00167 #endif
00168 
00170 #ifndef DO_EVERY
00171 #define DO_EVERY(secs, code) \
00172   DO_EVERY_TS(secs, pcl::getTime(), code)
00173 #endif
00174 
00175 }  // end namespace
00178 #endif  //#ifndef PCL_NORMS_H_


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:36:35