casacore
Timer.h
Go to the documentation of this file.
1 //# Timer.h: measure the time it takes to execute parts of a program
2 //# Copyright (C) 1993,1994,1995,1996,1997,1999,2000,2001
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_TIMER_H
29 #define CASA_TIMER_H
30 
31 
32 #include <casacore/casa/aips.h>
33 #include <sys/types.h>
34 
35 //# Forward declarations
36 #include <casacore/casa/iosfwd.h>
37 
38 #if defined(DOS) || defined(MSDOS)
39 #include <sys/timeb.h>
40 extern "C" {
41 #include <time.h>
42 }
43 
44 #elif defined(AIPS_SOLARIS) || defined(AIPS_IRIX) || defined(AIPS_OSF) || defined(__hpux__) || defined(AIPS_LINUX) || defined(AIPS_DARWIN) || defined(AIPS_BSD) || defined(__GLIBC__)
45  #if defined(AIPS_CRAY_PGI)
46  #include <sys/time.h>
47  #include <sys/resource.h>
48  #include <unistd.h>
49  extern "C" int getrusage(int, struct rusage*);
50  #else
51  #include <sys/times.h>
52  #include <unistd.h>
53  #endif
54 
55 #else
56 #include <sys/timeb.h>
57 #include <sys/time.h>
58 extern "C" int getrusage(int, struct rusage*);
59 extern "C" int ftime(struct timeb*);
60 #include <sys/resource.h>
61 #endif
62 
63 namespace casacore { //# NAMESPACE CASACORE - BEGIN
64 
65 // Class declaration.
66 class String;
67 
68 // <summary> measure the time it takes to execute parts of a program</summary>
69 
70 // <use visibility=export>
71 
72 // <reviewed reviewer="Paul Shannon" date="1995/03/01/ tests="tTimer" demos="">
73 // </reviewed>
74 
75 // <synopsis>
76 // The Timer class provides an interface to system timing. It
77 // allows a C++ program to record the time between a reference
78 // point (mark) and now. This class uses the system time(2)
79 // interface to provide time resolution at either millisecond or
80 // microsecond granularity, depending upon operating system
81 // support and features. Since the time duration is stored in
82 // a 32-bit word, the maximum time period before rollover
83 // occurs is about 71 minutes.
84 //
85 // Due to operating system dependencies, the accuracy of all
86 // member function results may not be as documented. For example
87 // some operating systems do not support timers with
88 // microsecond resolution. In those cases, the values returned
89 // are provided to the nearest millisecond or other unit of
90 // time as appropriate. See the Timer header file for system-
91 // specific notes.
92 //
93 // <note role=tip> This Timer class is based on the TI COOL library
94 // Timer class
95 // </note>
96 // </synopsis>
97 
98 // <example>
99 // Here's how to create a timer, start it (the 'mark' member function)
100 // and display a breakdown. Recall that
101 // <srcblock> realtime = user time + system time
102 // </srcblock>
103 // <srcblock>
104 //
105 // Timer timer; // the mark is set at construction time
106 // timer.mark(); // if you want to restart the clock
107 // ...do some calculation...
108 // cout << "user: " << timer.user () << endl;
109 // cout << "system: " << timer.system () << endl;
110 // cout << "real: " << timer.real () << endl;
111 //
112 // </srcblock>
113 // </example>
114 
115 // <todo asof="1995/03/01">
116 // <li> it might be useful to have a stop () member function: for
117 // really precise timing, the successive calls to user, system
118 // and real all happen at measurably different times
119 // <li> provide an enquiry function that reports the resolution of
120 // the timer
121 // <li> add 'start' member function, a synonym for 'mark' but more
122 // comprehensible
123 //
124 // </todo>
125 
126 
127 class Timer {
128 public:
129  //
130  // Construct a timer and set the mark ("mark()").
131  //
132  Timer() {mark();}
133 
134  //
135  // Set the timer mark -- i.e., start the clock ticking
136  //
137  void mark();
138 
139  //
140  // Get the user time (in seconds) since last "mark()".
141  //
142  double user() const;
143 
144  //
145  // Get the system time (in seconds) since last "mark()".
146  //
147  double system() const;
148 
149  //
150  // Get the user+system time (in seconds) since last "mark()".
151  //
152  double all() const;
153 
154  //
155  // Get the real time (in seconds) since last "mark()".
156  //
157  double real() const;
158 
159  // Show real, user, system time (in seconds) on cout or a user supplied
160  // stream.
161  // <group>
162  void show() const;
163  void show(ostream &os) const;
164  // </group>
165 
166  // Show real, user, system time (in seconds) on cout or a user supplied
167  // stream preceeded by the string parameter.
168  // <group>
169  void show(const String&) const;
170  void show(ostream &os, const String&prefix) const;
171  // </group>
172 
173  //
174  // Get the user time (in microseconds) since last "mark()".
175  //
176  double user_usec() const;
177 
178  //
179  // Get the system time (in microseconds) since last "mark()".
180  //
181  double system_usec() const;
182 
183  //
184  // Get the user+system time (in microseconds) since last "mark()".
185  //
186  double all_usec() const;
187 
188 private:
189 #if defined(DOS) || defined(MSDOS)
190  clock_t usage0;
191  timeb real0; //# elapsed real time at last mark
192 #elif defined(AIPS_SOLARIS) || defined(AIPS_IRIX) || defined(AIPS_OSF) || defined(__hpux__) || defined(AIPS_LINUX) || defined(AIPS_DARWIN) || defined(AIPS_BSD) || defined(__GLIBC__)
193  #if defined(AIPS_CRAY_PGI)
194  //struct timeval usage0;
195  rusage usage0; //# rusage structure at last mark
196  struct timeval real0; //# elapsed real time at last mark
197  #else
198  tms usage0; //# tms structure at last mark
199  clock_t real0; //# elapsed real time at last mark
200  #endif
201 #else
202  rusage usage0; //# rusage structure at last mark
203  timeb real0; //# elapsed real time at last mark
204 #endif
205 };
206 
207 
208 } //# NAMESPACE CASACORE - END
209 
210 #endif
int getrusage(int, struct rusage *)
int ftime(struct timeb *)
String: the storage and methods of handling collections of characters.
Definition: String.h:225
double system() const
Get the system time (in seconds) since last "mark()".
rusage usage0
Definition: Timer.h:202
void show(ostream &os) const
void mark()
Set the timer mark – i.e., start the clock ticking.
void show(ostream &os, const String &prefix) const
double system_usec() const
Get the system time (in microseconds) since last "mark()".
double all_usec() const
Get the user+system time (in microseconds) since last "mark()".
double user() const
Get the user time (in seconds) since last "mark()".
double real() const
Get the real time (in seconds) since last "mark()".
void show() const
Show real, user, system time (in seconds) on cout or a user supplied stream.
double user_usec() const
Get the user time (in microseconds) since last "mark()".
void show(const String &) const
Show real, user, system time (in seconds) on cout or a user supplied stream preceeded by the string p...
timeb real0
Definition: Timer.h:203
double all() const
Get the user+system time (in seconds) since last "mark()".
Timer()
Construct a timer and set the mark ("mark()").
Definition: Timer.h:132
this file contains all the compiler specific defines
Definition: mainpage.dox:28