timer.h

00001 /*
00002   -------------------------------------------------------------------
00003   
00004   Copyright (C) 2006, 2007, Andrew W. Steiner
00005   
00006   This file is part of O2scl.
00007   
00008   O2scl is free software; you can redistribute it and/or modify
00009   it under the terms of the GNU General Public License as published by
00010   the Free Software Foundation; either version 3 of the License, or
00011   (at your option) any later version.
00012   
00013   O2scl is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016   GNU General Public License for more details.
00017   
00018   You should have received a copy of the GNU General Public License
00019   along with O2scl. If not, see <http://www.gnu.org/licenses/>.
00020 
00021   -------------------------------------------------------------------
00022 */
00023 #ifndef O2SCL_TIMER_H
00024 #define O2SCL_TIMER_H
00025 
00026 #include <iostream>
00027 #include <string>
00028 #include <o2scl/string_conv.h>
00029 #include <o2scl/misc.h>
00030 #include <sys/time.h>
00031 
00032 #ifndef DOXYGENP
00033 namespace o2scl {
00034 #endif
00035 
00036   /** 
00037       \brief Provide an interface for timing execution using 
00038       \c gettimeofday()
00039 
00040       \todo Better testing which doesn't use a fixed number of
00041       mathematical operations, but automatically selects enough
00042       operations.
00043   */
00044   class timer_gettod {
00045   public:
00046     
00047 #ifdef NEVER_DEFINED
00048     
00049     // typical definition of timeval (for reference only)
00050     struct timeval {
00051       // seconds
00052       time_t         tv_sec;        
00053       // microseconds
00054       suseconds_t    tv_usec;  
00055     };
00056     
00057     // typical definition of timezone (for reference only)
00058     struct timezone {
00059       // minutes W of Greenwich
00060       int  tz_minuteswest; 
00061       // type of dst correction
00062       int  tz_dsttime;     
00063     };
00064     
00065 #endif
00066     
00067     timer_gettod() { reset(); }
00068     
00069     /// Set time 'zero'
00070     int reset() { return gettimeofday(&zero,&tz); }
00071 
00072     /// Store the present time
00073     int set() { return gettimeofday(&mark,&tz); }
00074     
00075     /// Return the number of seconds between set() and reset()
00076     double seconds_elapsed();
00077     
00078     /// Return the time between set() and reset()
00079     int time_elapsed(int &d, int &h, int &m, int &s, int &usec);
00080     
00081     /// Time remaining if \c n out of \c tot tasks have been completed
00082     int time_remaining(int n, int ntot, 
00083                        int &d, int &h, int &m, int &s, int &usec);
00084     
00085     /// Time remaining if \c n out of \c tot tasks have been completed
00086     std::string time_remaining(int n, int ntot);
00087     
00088     /// Convert a time interval to a string
00089     std::string interval_to_string(int d, int h, int m, int s, int usec);
00090 
00091   protected:
00092 
00093 #ifndef DOXYGEN_INTERNAL
00094 
00095     /// The last time the clock was reset
00096     struct timeval zero;
00097 
00098     /// The most resent time from set()
00099     struct timeval mark;
00100 
00101     /// The timezone
00102     struct timezone tz;
00103 
00104 #endif
00105 
00106   };
00107 
00108   /** 
00109       \brief Provide an interface for timing execution using 
00110       \c clock()
00111       
00112       \note Note that the time return by clock() is reset on
00113       some regular interval (sometimes 72 minutes) and this
00114       class does not yet account for this.
00115   */
00116   class timer_clock {
00117 
00118   public:
00119     timer_clock();
00120 
00121     /// Number of seconds elapsed
00122     double time_since();
00123 
00124     /** 
00125         \brief Time elapsed in days, hours, minutes, seconds, and 
00126         fractions of seconds
00127     */
00128     void time_since(int &d, int &h, int &m, int &s, double &f);
00129   
00130     /// Time remaining if \c n out of \c tot tasks have been completed
00131     void time_remaining(int n, int tot, int &d, int &h, int &m, int &s,
00132                         double &f);
00133 
00134     /// Convert a time interval to a string
00135     std::string interval_to_string(int d, int h, int m, int s, double f=0.0);
00136 
00137   protected:
00138 
00139 #ifndef DOXYGEN_INTERNAL
00140 
00141     /// Desc
00142     clock_t time;
00143 
00144 #endif
00145 
00146   };
00147 
00148 #ifndef DOXYGENP
00149 }
00150 #endif
00151 
00152 #endif

Documentation generated with Doxygen and provided under the GNU Free Documentation License. See License Information for details.

Project hosting provided by SourceForge.net Logo, O2scl Sourceforge Project Page