NAME
CPUclock - measures the CPU time in C++ programs
INCLUDE
include "CPUclock.h"
SYNTAX
class CPUclock
{
bool locked_clock; // true: getInterval does not reset the time
long tunits; // number of timing units per second for struct tms
#ifndef WIN32
tms last; // previous clock value
tms now; // present clock value
tms diff; // diff = now - last
#else
_timeb last; // previous clock value
_timeb now; // present clock value
_timeb diff; // diff = now - last
clock_t now_cpu;
clock_t last_cpu;
clock_t diff_cpu; // time used by this process
#endif
void swap(); // last = now
void subt(); // subtract: now - last, store in diff
double convert2sec (clock_t tm) // convert to seconds
{ return double(tm)/double(tunits); }
public:
CPUclock ();
// the increase in time (seconds) since last call to getInterval
// or to initTime ();
double getInterval (); // returns user time + system time
double getIntervalU (); // returns user time only
void initTime () { getInterval(); }
double getTime (); // return absolute user time, Win32 returning "now_cpu"
void lock () { locked_clock = true; }
void unlock () { locked_clock = false; }
bool locked () const { return locked_clock; }
};
KEYWORDS
CPU time, clock
DESCRIPTION
One can get the user time in absolute seconds, or the length of
intervals can be measured (in seconds).
getTime - returns absolute user time in seconds
initTime - start measuring the length of an interval (typically
called prior to the computational job to be measured). In fact,
initTime is just a call to getInterval - the function was for
making programs easier to read.
getInterval - returns the length of the user+system time interval
(in seconds) between the present call to getInterval and the last
call to initTime or to getInterval.
getIntervalU - as getInterval, but only the user time is
returned.
EXAMPLE
| #include <CPUclock.h>
class MySolver
{
...
CPUclock cpu_clock;
...
};
void MySolver::solveProblem()
{
cpu_clock.getInterval(); // initialize
// call various solve functionality
real cpu_time = cpu_clock.getInterval() // CPU time since last
call
s_o << "solveProblem used " << cpu_time " seconds0;
}
REMARKS
Some utility classes in Diffpack, like FEM and LinEqAdm, have
built-in CPUclock data members. Simulators derived from class FEM
inherit the real variable cpu_time_makeSystem, which contains the
CPU time consumption in the last call to makeSystem. If a solver
employs a lineq handle to a LinEqAdm object for solving linear
systems, the call lineq->getCPUtime4solve() returns the CPU time
required by the execution of the linear system solution proce
dure, i.e., the last lineq->solve() call. The CPU time for assem
bly and solve processes are automatically reported in the case
name.dp file.
SEE ALSO
"time.h, sys/times.h
DEVELOPED BY
SINTEF Applied Mathematics, Oslo, Norway, and University of Oslo,
Dept. of Mathematics, Norway
AUTHOR
Hans Petter Langtangen, SINTEF/UiO