STIR  6.2.0
List of all members
stir::CPUTimer Class Reference

A class for measuring elapsed CPU time. More...

#include "stir/CPUTimer.h"

Inheritance diagram for stir::CPUTimer:
Inheritance graph
[legend]

Additional Inherited Members

- Public Member Functions inherited from stir::Timer
void start (bool do_reset=false)
 start stopwatch, optionally resetting first More...
 
void stop ()
 stop stopwatch
 
void reset ()
 reset stopwatch More...
 
double value () const
 return value is undefined when start() is not called first.
 
- Protected Attributes inherited from stir::Timer
bool running
 
double previous_value
 
double previous_total_value
 

Detailed Description

A class for measuring elapsed CPU time.

CPUTimer is derived from Timer, and hence has the same interface. It returns the amount of CPU time (in secs) used by the current process and its children.

Warning
If too long CPU times are measured, wrap-around will occur.
Currently, CPUTimer returns garbage on Win95 (and probably Win98). It does work fine on NT, XP etc though.
Implementation details

For most systems, CPUTimer::value() returns elapsed CPU time using the ANSI clock() function. From the man page:

The reported time is the sum
of the CPU time of the calling process and its terminated child
processes for which it has executed wait, system, or pclose
subroutines.

clock() has a possible problem of wrap-around. For many Unix systems this occurs very soon. For a SUN, the man page states this is every 2147 secs of CPU time (36 minutes !). AIX should be similar, as both have CLOCKS_PER_SEC = 10e6, and clock_t == int == 32 bit.

So, since version 1.2 the times() function (with the rms_utime field) is used if defined(OS_UNIX). The man page for times() states something very similar to clock():

tms_utime : The CPU time used for executing instructions in the user space of the calling process.

This information is read from the calling process as well as from each completed child process for which the calling process executed a wait subroutine.

As times() returns results measured in clock interrupt clicks (on AIX 100 per second), wrap around occurs much later.

Finally, VC++ 5.0 seems to have a bug in clock() that makes its results dependent on the load of the system (i.e. it is more like 'wall clock time'). We now use GetProcessTimes() (with the lpUserTime variable) instead. The documentation states:

... the amount of time that the process has executed in user mode. The time that each of the threads of the process has executed in user mode is determined, and then all of those times are summed together to obtain this value.

Warning: this only works for NT, not for Win95


The documentation for this class was generated from the following files: