
00001 /*!@file Util/Timer.H A simple class for precise time measurements */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Laurent Itti <itti@usc.edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Util/Timer.H $ 00035 // $Id: Timer.H 5504 2005-09-09 22:48:37Z rjpeters $ 00036 // 00037 00038 #ifndef TIMER_H_DEFINED 00039 #define TIMER_H_DEFINED 00040 00041 #include "Util/SimTime.H" 00042 #include "Util/Types.H" // for uint64 00043 #include "Util/log.H" 00044 #include <sys/time.h> 00045 #include <unistd.h> 00046 00047 //! This class implements a real-time timer 00048 /*! Timing starts at construction or reset(). 00049 00050 At construction, you can decide on a number of timer ticks per second; 00051 use 1000 to count in milliseconds, 1000000 to count in microseconds, 00052 etc. The best resolution achievable is 1us, as this timer relies on 00053 the gettimeofday() system call, which has microsecond resolution. Use 00054 get() to measure the number of elapsed ticks since last reset(), or 00055 getSecs() to get the fractional number of seconds. Use ticksPerSec() 00056 to retrieve this value, for example to convert yourself your measured 00057 times back to seconds instead of using getSecs(). */ 00058 class Timer { 00059 public: 00060 //! Constructor; immediately start timing 00061 /*! @param tickspersec number of timer ticks per second; use 1000 to 00062 count in milliseconds, 1000000 to count in microseconds, 00063 etc. Values larger than 1000000 will throw a fatal error. If 00064 1000000/tickspersec is not integer, a fatal error will also 00065 occur. */ 00066 inline Timer(const int tickspersec = 1000); 00067 00068 //! Reset the timer and immediately start timing again 00069 inline void reset(); 00070 00071 //! Return elapsed time 00072 /*! Return value is the number of ticks since last reset or since 00073 construction. */ 00074 inline uint64 get() const; 00075 00076 //! Return elapsed time since last reset, and reset 00077 inline uint64 getReset(); 00078 00079 //! Returns number of ticks per second, as specified at construction 00080 inline int ticksPerSec() const; 00081 00082 //! Returns number of seconds since last reset 00083 inline double getSecs() const; 00084 00085 //! Returns the elapsed SimTime since last reset 00086 inline SimTime getSimTime() const; 00087 00088 protected: 00089 //! time unit multipliers 00090 int64 div, mul; 00091 //! timestamp of last reset() 00092 struct timeval tv; 00093 }; 00094 00095 // ###################################################################### 00096 // ##### INLINE FUNCTIONS: 00097 // ###################################################################### 00098 00099 inline Timer::Timer(const int tickspersec) : 00100 div(1000000LL / int64(tickspersec)), mul(int64(tickspersec)) 00101 { 00102 if (tickspersec > 1000000LL) LFATAL("tickspersec too large"); 00103 if (1000000LL % tickspersec) LFATAL("1000000/tickspersec not integer"); 00104 reset(); 00105 } 00106 00107 // ###################################################################### 00108 inline void Timer::reset() 00109 { gettimeofday(&tv, NULL); } 00110 00111 // ###################################################################### 00112 inline uint64 Timer::get() const 00113 { 00114 struct timeval tv2; gettimeofday(&tv2, NULL); 00115 return uint64(int64(tv2.tv_usec - tv.tv_usec) / div + 00116 int64(tv2.tv_sec - tv.tv_sec ) * mul); 00117 } 00118 00119 // ###################################################################### 00120 inline uint64 Timer::getReset() 00121 { 00122 struct timeval tv2; gettimeofday(&tv2, NULL); 00123 uint64 ret = uint64(int64(tv2.tv_usec - tv.tv_usec) / div + 00124 int64(tv2.tv_sec - tv.tv_sec ) * mul); 00125 tv.tv_sec = tv2.tv_sec; tv.tv_usec = tv2.tv_usec; 00126 return ret; 00127 } 00128 00129 // ###################################################################### 00130 inline int Timer::ticksPerSec() const 00131 { return int(mul); } 00132 00133 // ###################################################################### 00134 inline double Timer::getSecs() const 00135 { 00136 struct timeval tv2; gettimeofday(&tv2, NULL); 00137 return double(tv2.tv_usec - tv.tv_usec) / 1000000.0 + 00138 double(tv2.tv_sec - tv.tv_sec); 00139 } 00140 00141 // ###################################################################### 00142 inline SimTime Timer::getSimTime() const 00143 { 00144 return SimTime::SECS(this->getSecs()); 00145 } 00146 00147 #endif 00148 00149 // ###################################################################### 00150 /* So things look consistent in everyone's emacs... */ 00151 /* Local Variables: */ 00152 /* indent-tabs-mode: nil */ 00153 /* End: */
1.4.4