Point Cloud Library (PCL)  1.3.1
time.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  */
00035 
00036 #ifndef PCL_TIME_H_
00037 #define PCL_TIME_H_
00038 
00039 #include <cmath>
00040 #include <string>
00041 #include <boost/date_time/posix_time/posix_time.hpp>
00042 
00050 namespace pcl
00051 {
00052 
00057   class StopWatch
00058   {
00059     public:
00060       StopWatch()
00061       {
00062         start_time_ = boost::posix_time::microsec_clock::local_time();
00063       }
00064 
00066       inline float
00067       getTime()
00068       {
00069         boost::posix_time::ptime end_time = boost::posix_time::microsec_clock::local_time();
00070         return (float)((end_time - start_time_).total_milliseconds());
00071       }
00072 
00074       inline float
00075       getTimeSeconds()
00076       {
00077         return (getTime() * 0.001f);
00078       }
00079 
00081       inline void
00082       reset()
00083       {
00084         start_time_ = boost::posix_time::microsec_clock::local_time();
00085       }
00086 
00087     protected:
00088       boost::posix_time::ptime start_time_;
00089   };
00090 
00098   class ScopeTime : public StopWatch
00099   {
00100     public:
00101       inline ScopeTime (const char* title)
00102       {
00103         title_ = std::string(title);
00104       }
00105 
00106       inline ~ScopeTime ()
00107       {
00108         std::cerr << title_ << " took " << getTime() << "ms.\n";
00109       }
00110 
00111     private:
00112       std::string title_;
00113   };
00114 
00115 
00116 #ifndef MEASURE_FUNCTION_TIME
00117 #define MEASURE_FUNCTION_TIME \
00118   ScopeTime scopeTime(__func__)
00119 #endif
00120 
00121 inline double getTime ()
00122 {
00123   boost::posix_time::ptime epoch_time(boost::gregorian::date(1970,1,1));
00124   boost::posix_time::ptime current_time = boost::posix_time::microsec_clock::local_time();
00125   return (current_time - epoch_time).total_nanoseconds() * 1.0e-9;
00126 }
00127 
00129 #ifndef DO_EVERY_TS
00130 #define DO_EVERY_TS(secs, currentTime, code) \
00131 if (1) {\
00132   static double s_lastDone_ = 0.0; \
00133   double s_now_ = (currentTime); \
00134   if (s_lastDone_ > s_now_) \
00135     s_lastDone_ = s_now_; \
00136   if ((s_now_ - s_lastDone_) > (secs)) {        \
00137     code; \
00138     s_lastDone_ = s_now_; \
00139   }\
00140 } else \
00141   (void)0
00142 #endif
00143 
00145 #ifndef DO_EVERY
00146 #define DO_EVERY(secs, code) \
00147   DO_EVERY_TS(secs, pcl::getTime(), code)
00148 #endif
00149 
00150 }  // end namespace
00153 #endif  //#ifndef PCL_NORMS_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines