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
30#include <casacore/casa/aips.h>
31#include <sys/types.h>
32
33//# Forward declarations
34#include <casacore/casa/iosfwd.h>
35
36#if defined(DOS) || defined(MSDOS)
37#include <sys/timeb.h>
38extern "C" {
39#include <time.h>
40}
41
42#elif defined(AIPS_SOLARIS) || defined(AIPS_IRIX) || defined(AIPS_OSF) || defined(__hpux__) || 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>
47 extern "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
124
125class Timer {
126public:
127 //
128 // Construct a timer and set the mark ("mark()").
129 //
130 Timer() {mark();}
131
132 //
133 // Set the timer mark -- i.e., start the clock ticking
134 //
135 void mark();
136
137 //
138 // Get the user time (in seconds) since last "mark()".
139 //
140 double user() const;
141
142 //
143 // Get the system time (in seconds) since last "mark()".
144 //
145 double system() const;
146
147 //
148 // Get the user+system time (in seconds) since last "mark()".
149 //
150 double all() const;
151
152 //
153 // Get the real time (in seconds) since last "mark()".
154 //
155 double real() const;
156
157 // Show real, user, system time (in seconds) on cout or a user supplied
158 // stream.
159 // <group>
160 void show() const;
161 void show(ostream &os) const;
162 // </group>
163
164 // Show real, user, system time (in seconds) on cout or a user supplied
165 // stream preceeded by the string parameter.
166 // <group>
167 void show(const String&) const;
168 void show(ostream &os, const String&prefix) const;
169 // </group>
170
171 //
172 // Get the user time (in microseconds) since last "mark()".
173 //
174 double user_usec() const;
175
176 //
177 // Get the system time (in microseconds) since last "mark()".
178 //
179 double system_usec() const;
180
181 //
182 // Get the user+system time (in microseconds) since last "mark()".
183 //
184 double all_usec() const;
185
186private:
187#if defined(DOS) || defined(MSDOS)
188 clock_t usage0;
189 timeb real0; //# elapsed real time at last mark
190#elif defined(AIPS_SOLARIS) || defined(AIPS_IRIX) || defined(AIPS_OSF) || defined(__hpux__) || 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
206} //# NAMESPACE CASACORE - END
207
208#endif
int getrusage(int, struct rusage *)
int ftime(struct timeb *)
String: the storage and methods of handling collections of characters.
Definition String.h:223
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:130
this file contains all the compiler specific defines
Definition mainpage.dox:28