| 
 |  | 
   #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();
   };
   }
There are three kinds of execution time that programmers may be interested in knowing about:
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.
Stopwatch(); A stopped Stopwatch (status()==0) for which system(), user(), and real() all read zero.
~Stopwatch(); Destructor
Stopwatch(const Stopwatch& s);
Stopwatch& operator=(const Stopwatch& s); Copying or assigning a Stopwatch creates a copy of its value.
int status()const; Returns 1 if the Stopwatch is running and 0 if it is stopped.
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.
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.
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.
Stopwatches encapsulate the facilities of times(2).
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";
       }