Utilities Package  Version Linux2023.0
Public Member Functions | List of all members
Scobey::Stopwatch Class Reference

For measuring the time taken by an algorithm to perform its task. More...

#include <utilities.h>

Public Member Functions

 Stopwatch ()
 Default constructor. More...
 
 Stopwatch (const string nameOfEventToBeTimed)
 Constructor which allows the naming of the event to be timed. More...
 
void setEventName (const string nameOfEventToBeTimed)
 Sets, or resets, the name of the event to be timed. More...
 
string getEventName () const
 Gets the current name of the event to be timed. More...
 
void start ()
 Causes the stopwatch to start running. More...
 
void stop ()
 Causes the stopwatch to stop running. More...
 
void delay (int delayFactor=1) const
 Adds some multiple of an artifical amount of time to the accumulated time on the stopwatch. More...
 
clock_t getTicks () const
 Gets the number of clock ticks between the most recent calls of start() and stop(). More...
 
double getSeconds () const
 Gets the number of seconds between the last calls of start() and stop(). More...
 
double getMinutes () const
 Gets the number of minutes between the last calls of start() and stop(). More...
 
double getHours () const
 Gets the number of hours between the last calls of start() and stop(). More...
 
void display () const
 

Detailed Description

For measuring the time taken by an algorithm to perform its task.

An object of class Stopwatch will behave like a stopwatch. It can be started, stopped, and asked for the time elapsed. This time can be computer time (clock ticks), or real time as measured in seconds, minutes, or hours.

Constructor & Destructor Documentation

◆ Stopwatch() [1/2]

Scobey::Stopwatch::Stopwatch ( )

Default constructor.

Precondition
None
Postcondition
The clock time at the time of declaration has been recorded.

◆ Stopwatch() [2/2]

Scobey::Stopwatch::Stopwatch ( const string  nameOfEventToBeTimed)

Constructor which allows the naming of the event to be timed.

Precondition
None
Postcondition
The clock time at the time of declaration has been recorded, as well as the name of the event to be timed.

Member Function Documentation

◆ setEventName()

void Scobey::Stopwatch::setEventName ( const string  nameOfEventToBeTimed)

Sets, or resets, the name of the event to be timed.

Returns
void
Precondition
None
Postcondition
The name of the event to be timed has been set to be the text contained in nameOfEventToBeTimed, which may be the empty string if no name is desired.

◆ getEventName()

string Scobey::Stopwatch::getEventName ( ) const

Gets the current name of the event to be timed.

Returns
The current name of the event to be timed.
Precondition
None
Postcondition
No side effects.

◆ start()

void Scobey::Stopwatch::start ( )

Causes the stopwatch to start running.

Returns
void
Precondition
stop() has been called, or start() has never been called.
Postcondition
The stopwatch timer has been "started" and the clock time at start time has been recorded, in computer clock ticks.

◆ stop()

void Scobey::Stopwatch::stop ( )

Causes the stopwatch to stop running.

Returns
void
Precondition
start() has been called.
Postcondition
The stopwatch timer has been "stopped" and the accumulated time since start() was called has been recorded.

◆ delay()

void Scobey::Stopwatch::delay ( int  delayFactor = 1) const

Adds some multiple of an artifical amount of time to the accumulated time on the stopwatch.

Returns
void
Precondition
start() has been called.
Postcondition
An artificial amount of time has been added to the total computer process time as reported by the clock function. In other words, during this delay time the "stopwatch" continues to accumulate time but nothing else happens. Thus this is a way to make an operation appear to take longer than it actually does. It can be used to cause the stopwatch to accumulate "artificial" time when monitoring the performance of an algorithm on a relatively small data set. This helps us to overcome the problem of trying to measure the amount of time taken to perform a calulation and finding that it took "no time at all". Note that we still cannot measure the absoute time taken, but fortunately we are often only interested in measuring the relative time taken by the same algorithm on data sets of different sizes, or by different algorithms on data sets of the same size. The input parameter delayFactor is a linear multiplier for increasing the amount of delay time. For example, a delayFactor of 2 doubles the amount of delay time accumulated by a single call to the delay() member function.

◆ getTicks()

clock_t Scobey::Stopwatch::getTicks ( ) const

Gets the number of clock ticks between the most recent calls of start() and stop().

Returns
The number of clock ticks between the last calls to start() and stop().
Precondition
start() and stop() have been called (in that order).
Postcondition
The accumulated time, in computer clock ticks, between the times when the stopwatch was last started and stopped, has been returned.

◆ getSeconds()

double Scobey::Stopwatch::getSeconds ( ) const

Gets the number of seconds between the last calls of start() and stop().

Returns
The number of seconds between the last calls to start() and stop().
Precondition
start() and stop() have been called (in that order).
Postcondition
The accumulated time, in seconds, between the times when the stopwatch was last started and stopped, has been returned.

◆ getMinutes()

double Scobey::Stopwatch::getMinutes ( ) const

Gets the number of minutes between the last calls of start() and stop().

Returns
The number of minutes between the last calls to start() and stop().
Precondition
start() and stop() have been called (in that order).
Postcondition
The accumulated time, in minutes, between the times when the stopwatch was last started and stopped, has been returned.

◆ getHours()

double Scobey::Stopwatch::getHours ( ) const

Gets the number of hours between the last calls of start() and stop().

Returns
The number of hours between the last calls to start() and stop().
Precondition
start() and stop() have been called (in that order).
Postcondition
The accumulated time, in hours, between the times when the stopwatch was last started and stopped, has been returned.

◆ display()

void Scobey::Stopwatch::display ( ) const
Displays the time, in hours (as an integer value), minutes (as an
integer value), and seconds (as a double value), between the last
time this <tt>%Stopwatch</tt> object was started and stopped.
@pre <em>start()</em> and <em>stop()</em> must have been called,
in that order.
@post
- Case 1: If no name has been given to the event that has just been
timed, the ouput displayed is this:
Total time elapsed = hh:mm:ss.sss
  • Case 2: If a name (name-of-event) has been given to the event that has just been timed, the ouput displayed is this:
    Total time elapsed for event name-of-event = hh:mm:ss.sss
    

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