casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Timer.h
Go to the documentation of this file.
00001 //# Timer.h:  measure the time it takes to execute parts of a program
00002 //# Copyright (C) 1993,1994,1995,1996,1997,1999,2000,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: Timer.h 20734 2009-09-28 23:44:40Z Malte.Marquarding $
00027 
00028 #ifndef CASA_TIMER_H
00029 #define CASA_TIMER_H
00030 
00031 
00032 #include <casa/aips.h>
00033 #include <sys/types.h>
00034 
00035 //# Forward declarations
00036 #include <casa/iosfwd.h>
00037 
00038 #if defined(DOS) || defined(MSDOS)
00039 #include <sys/timeb.h>
00040 extern "C" {
00041 #include <time.h>
00042 }
00043 
00044 #elif defined(AIPS_SOLARIS) || defined(AIPS_IRIX) || defined(AIPS_OSF) || defined(__hpux__) || defined(AIPS_LINUX) || defined(AIPS_DARWIN) || defined(AIPS_BSD)
00045   #if defined(AIPS_CRAY_PGI)
00046     #include <sys/time.h>
00047     #include <sys/resource.h>
00048     #include <unistd.h>
00049     extern "C" int getrusage(int, struct rusage*);
00050   #else
00051     #include <sys/times.h>
00052     #include <unistd.h>
00053   #endif
00054 
00055 #else
00056 #include <sys/timeb.h>
00057 #include <sys/time.h>
00058 extern "C" int getrusage(int, struct rusage*);
00059 extern "C" int ftime(struct timeb*);
00060 #include <sys/resource.h>
00061 #endif
00062 
00063 namespace casa { //# NAMESPACE CASA - BEGIN
00064 
00065 // Class declaration.
00066 class String;
00067 
00068 // <summary> measure the time it takes to execute parts of a program</summary>
00069 
00070 // <use visibility=export>
00071 
00072 // <reviewed reviewer="Paul Shannon" date="1995/03/01/ tests="tTimer" demos="">
00073 // </reviewed>
00074 
00075 // <synopsis>
00076 // The Timer class  provides an interface to system timing.  It
00077 // allows a C++ program to record the time between a reference
00078 // point  (mark) and  now.  This  class  uses the system time(2)
00079 // interface to provide time resolution at either millisecond or
00080 // microsecond granularity,  depending  upon  operating  system
00081 // support and features. Since the time duration  is  stored  in
00082 // a  32-bit word,  the  maximum  time  period  before rollover 
00083 // occurs is about 71 minutes.
00084 //
00085 // Due to operating system dependencies, the  accuracy  of  all
00086 // member  function results may not be as documented. For example
00087 // some operating  systems  do  not  support  timers  with
00088 // microsecond  resolution. In those cases, the values returned
00089 // are provided to the nearest millisecond  or  other  unit  of
00090 // time  as  appropriate. See the Timer header file for system-
00091 // specific notes.
00092 //
00093 // <note role=tip> This Timer class is based on the TI COOL library 
00094 //        Timer class
00095 // </note>
00096 // </synopsis>
00097 
00098 // <example>
00099 // Here's how to create a timer, start it (the 'mark' member function)
00100 // and display a breakdown.  Recall that 
00101 // <srcblock>        realtime = user time  + system time
00102 // </srcblock>
00103 // <srcblock>
00104 // 
00105 //  Timer timer;   // the mark is set at construction time
00106 //  timer.mark();  // if you want to restart the clock
00107 //   ...do some calculation...
00108 //   cout << "user:   " << timer.user () << endl; 
00109 //   cout << "system: " << timer.system () << endl;
00110 //   cout << "real:   " << timer.real () << endl; 
00111 //
00112 // </srcblock>
00113 //  </example>
00114 
00115 // <todo asof="1995/03/01">
00116 //   <li> it might be useful to have a stop () member function:  for
00117 //        really precise timing, the successive calls to user, system
00118 //        and real all happen at measurably different times
00119 //   <li> provide an enquiry function that reports the resolution of
00120 //        the timer
00121 //   <li> add 'start' member function, a synonym for 'mark' but more
00122 //        comprehensible
00123 //        
00124 // </todo>
00125 
00126 
00127 class Timer {
00128 public:
00129   //
00130   // Construct a timer and set the mark ("mark()").
00131   //
00132   Timer() {mark();}
00133 
00134   //
00135   // Set the timer mark -- i.e., start the clock ticking
00136   //
00137   void mark();
00138 
00139   //
00140   // Get the user time (in seconds) since last "mark()".
00141   //
00142   double user() const;
00143 
00144   //
00145   // Get the system time (in seconds) since last "mark()".
00146   //
00147   double system() const;
00148 
00149   //
00150   // Get the user+system time (in seconds) since last "mark()".
00151   //
00152   double all() const;
00153 
00154   //
00155   // Get the real time (in seconds) since last "mark()".
00156   //
00157   double real() const;
00158 
00159   // Show real, user, system time (in seconds) on cout or a user supplied
00160   // stream.
00161   // <group>
00162   void show() const;
00163   void show(ostream &os) const;
00164   // </group>
00165 
00166   // Show real, user, system time (in seconds) on cout or a user supplied
00167   // stream preceeded by the string parameter.
00168   // <group>
00169   void show(const String&) const;
00170   void show(ostream &os, const String&prefix) const;
00171   // </group>
00172 
00173   //
00174   // Get the user time (in microseconds) since last "mark()".
00175   //
00176   double user_usec() const;
00177 
00178   //
00179   // Get the system time (in microseconds) since last "mark()".
00180   //
00181   double system_usec() const;
00182 
00183   //
00184   // Get the user+system time (in microseconds) since last "mark()".
00185   //
00186   double all_usec() const;
00187 
00188 private:
00189 #if defined(DOS) || defined(MSDOS)
00190     clock_t usage0;
00191     timeb   real0;          //# elapsed real time at last mark
00192 #elif defined(AIPS_SOLARIS) || defined(AIPS_IRIX) || defined(AIPS_OSF) || defined(__hpux__) || defined(AIPS_LINUX) || defined(AIPS_DARWIN) || defined(AIPS_BSD)
00193   #if defined(AIPS_CRAY_PGI)
00194     //struct timeval usage0;
00195     rusage usage0;          //# rusage structure at last mark
00196     struct timeval real0;   //# elapsed real time at last mark
00197   #else
00198     tms     usage0;         //# tms structure at last mark
00199     clock_t real0;          //# elapsed real time at last mark
00200   #endif
00201 #else
00202     rusage  usage0;         //# rusage structure at last mark
00203     timeb   real0;          //# elapsed real time at last mark
00204 #endif
00205 };
00206 
00207 
00208 } //# NAMESPACE CASA - END
00209 
00210 #endif