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


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:18:47