Stopwatch(3C++)


Stopwatch -- program execution time measurement

Synopsis

   #include <Stopwatch.h>
   namespace SCO_SC {
   

class Stopwatch{ public: // Constructors, destructor Stopwatch(); ~Stopwatch(); // Copy and assign Stopwatch(const Stopwatch& s); Stopwatch& operator=(const Stopwatch& s); // Status int status()const; // Setting operations void start(); void stop(); void reset(); // Reading operations double system()const; double user()const; double real()const; // Miscellaneous static double resolution(); }; }

Description

There are three kinds of execution time that programmers may be interested in knowing about:

Think of a Stopwatch as a device with three buttons (labeled start, stop, and reset) and three digital displays (labeled system, user, and real). All three displays initially read zero. When start is pressed, measurements begin accumulating in all three displays. When stop is pressed, all three displays cease accumulating. When reset is pressed, all three displays are (almost) instantaneously cleared; if the Stopwatch was running when reset was pressed, the displays resume accumulating immediately after resetting. Any display may be read at any time, regardless of whether a Stopwatch is running or stopped.

For purposes of estimating---and subsequently eliminating---the bias inherent in a reading due to act of measurement, it can be safely assumed that every Stopwatch operation includes a (user time) overhead on the order of a function call. Some operations also incur a (system time) overhead on the order of a system call; these are noted below.

Constructors, destructor

Stopwatch(); A stopped Stopwatch (status()==0) for which system(), user(), and real() all read zero.

~Stopwatch(); Destructor

Copy and assign

Stopwatch(const Stopwatch& s);

Stopwatch& operator=(const Stopwatch& s); Copying or assigning a Stopwatch creates a copy of its value.

Status

int status()const; Returns 1 if the Stopwatch is running and 0 if it is stopped.

Setting operations

void start(); If the Stopwatch is already running, the call has no effect. If the Stopwatch is stopped, it starts running and measurement resumes from where it left off. Requires a system call if the status changes from stopped to running.

void stop(); If the Stopwatch is already stopped, the call has no effect. If the Stopwatch is running, it stops running and measurement is halted. Requires a system call if the status changes from running to stopped.

void reset(); Resets system(), user(), and real() to zero. The status is not affected by this call; that is, if the Stopwatch is running, it continues running uninterrupted after resetting, and if it is stopped, it remains stopped after resetting. Always requires a system call.

Reading operations

double system()const;

double user()const;

double real()const; Returns the system time, user time, and real time, respectively, in units of seconds. Requires a system call if the Stopwatch is running.

Miscellaneous

static double resolution(); Returns the (system-dependent) resolution of Stopwatches in units of seconds. On a system where resolution() is 1/60 second (a typical value), Stopwatch readings are accurate only to the nearest 1/60 second.

Notes

Stopwatches encapsulate the facilities of times(2).

Example

The following program computes the user time for a computation of interest. Taking an average over N repetitions decreases the effect of Stopwatch inaccuracy to an acceptable level:

       #include <Stopwatch.h>
       #include <stream.h>
       const int N = 10000;
       main(){
           Stopwatch w;
           w.start();
           for(int i=0;i<N;i++){
               computation of interest
           }
           w.stop();
           cout << "user time=" << w.user()/N
   << "\n";
       }

References

times(2)
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004