casacore
Loading...
Searching...
No Matches
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: casa-feedback@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#ifndef CASA_TIMER_H
27#define CASA_TIMER_H
28
29#include <casacore/casa/aips.h>
30#include <sys/types.h>
31
32// # Forward declarations
33#include <casacore/casa/iosfwd.h>
34
35#if defined(DOS) || defined(MSDOS)
36#include <sys/timeb.h>
37extern "C" {
38#include <time.h>
39}
40
41#elif defined(AIPS_SOLARIS) || defined(AIPS_IRIX) || defined(AIPS_OSF) || defined(__hpux__) || \
42 defined(AIPS_LINUX) || defined(AIPS_DARWIN) || defined(AIPS_BSD) || defined(__GLIBC__)
43#if defined(AIPS_CRAY_PGI)
44#include <sys/time.h>
45#include <sys/resource.h>
46#include <unistd.h>
47extern "C" int getrusage(int, struct rusage *);
48#else
49#include <sys/times.h>
50#include <unistd.h>
51#endif
52
53#else
54#include <sys/timeb.h>
55#include <sys/time.h>
56extern "C" int getrusage(int, struct rusage *);
57extern "C" int ftime(struct timeb *);
58#include <sys/resource.h>
59#endif
60
61namespace casacore { // # NAMESPACE CASACORE - BEGIN
62
63// Class declaration.
64class String;
65
66// <summary> measure the time it takes to execute parts of a program</summary>
67
68// <use visibility=export>
69
70// <reviewed reviewer="Paul Shannon" date="1995/03/01/ tests="tTimer" demos="">
71// </reviewed>
72
73// <synopsis>
74// The Timer class provides an interface to system timing. It
75// allows a C++ program to record the time between a reference
76// point (mark) and now. This class uses the system time(2)
77// interface to provide time resolution at either millisecond or
78// microsecond granularity, depending upon operating system
79// support and features. Since the time duration is stored in
80// a 32-bit word, the maximum time period before rollover
81// occurs is about 71 minutes.
82//
83// Due to operating system dependencies, the accuracy of all
84// member function results may not be as documented. For example
85// some operating systems do not support timers with
86// microsecond resolution. In those cases, the values returned
87// are provided to the nearest millisecond or other unit of
88// time as appropriate. See the Timer header file for system-
89// specific notes.
90//
91// <note role=tip> This Timer class is based on the TI COOL library
92// Timer class
93// </note>
94// </synopsis>
95
96// <example>
97// Here's how to create a timer, start it (the 'mark' member function)
98// and display a breakdown. Recall that
99// <srcblock> realtime = user time + system time
100// </srcblock>
101// <srcblock>
102//
103// Timer timer; // the mark is set at construction time
104// timer.mark(); // if you want to restart the clock
105// ...do some calculation...
106// cout << "user: " << timer.user () << endl;
107// cout << "system: " << timer.system () << endl;
108// cout << "real: " << timer.real () << endl;
109//
110// </srcblock>
111// </example>
112
113// <todo asof="1995/03/01">
114// <li> it might be useful to have a stop () member function: for
115// really precise timing, the successive calls to user, system
116// and real all happen at measurably different times
117// <li> provide an enquiry function that reports the resolution of
118// the timer
119// <li> add 'start' member function, a synonym for 'mark' but more
120// comprehensible
121//
122// </todo>
123
124class Timer {
125 public:
126 //
127 // Construct a timer and set the mark ("mark()").
128 //
129 Timer() { mark(); }
130
131 //
132 // Set the timer mark -- i.e., start the clock ticking
133 //
134 void mark();
135
136 //
137 // Get the user time (in seconds) since last "mark()".
138 //
139 double user() const;
140
141 //
142 // Get the system time (in seconds) since last "mark()".
143 //
144 double system() const;
145
146 //
147 // Get the user+system time (in seconds) since last "mark()".
148 //
149 double all() const;
150
151 //
152 // Get the real time (in seconds) since last "mark()".
153 //
154 double real() const;
155
156 // Show real, user, system time (in seconds) on cout or a user supplied
157 // stream.
158 // <group>
159 void show() const;
160 void show(ostream &os) const;
161 // </group>
162
163 // Show real, user, system time (in seconds) on cout or a user supplied
164 // stream preceeded by the string parameter.
165 // <group>
166 void show(const String &) const;
167 void show(ostream &os, const String &prefix) const;
168 // </group>
169
170 //
171 // Get the user time (in microseconds) since last "mark()".
172 //
173 double user_usec() const;
174
175 //
176 // Get the system time (in microseconds) since last "mark()".
177 //
178 double system_usec() const;
179
180 //
181 // Get the user+system time (in microseconds) since last "mark()".
182 //
183 double all_usec() const;
184
185 private:
186#if defined(DOS) || defined(MSDOS)
187 clock_t usage0;
188 timeb real0; // # elapsed real time at last mark
189#elif defined(AIPS_SOLARIS) || defined(AIPS_IRIX) || defined(AIPS_OSF) || defined(__hpux__) || \
190 defined(AIPS_LINUX) || defined(AIPS_DARWIN) || defined(AIPS_BSD) || defined(__GLIBC__)
191#if defined(AIPS_CRAY_PGI)
192 // struct timeval usage0;
193 rusage usage0; // # rusage structure at last mark
194 struct timeval real0; // # elapsed real time at last mark
195#else
196 tms usage0; // # tms structure at last mark
197 clock_t real0; // # elapsed real time at last mark
198#endif
199#else
200 rusage usage0; // # rusage structure at last mark
201 timeb real0; // # elapsed real time at last mark
202#endif
203};
204
205} // namespace casacore
206
207#endif
int getrusage(int, struct rusage *)
int ftime(struct timeb *)
String: the storage and methods of handling collections of characters.
Definition String.h:355
double system() const
Get the system time (in seconds) since last "mark()".
rusage usage0
Definition Timer.h:200
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:201
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:129
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28