OpenMS  2.8.0
Classes | Public Member Functions | List of all members
StopWatch Class Reference

This class is used to determine the current process' CPU (user and/or kernel) and wall time. More...

#include <OpenMS/SYSTEM/StopWatch.h>

Collaboration diagram for StopWatch:
[legend]

Classes

struct  TimeDiff_
 

Public Member Functions

void start ()
 Start the stop watch. More...
 
void stop ()
 Stop the stop watch (can be resumed later). If the stop watch was not running an exception is thrown. More...
 
void resume ()
 Resume a stopped StopWatch. More...
 
void reset ()
 Clear the stop watch but keep running. More...
 
void clear ()
 
Readout of the StopWatch
double getClockTime () const
 
double getUserTime () const
 
double getSystemTime () const
 
double getCPUTime () const
 

Predicates

typedef clock_t TimeType
 
static const PointerSizeInt cpu_speed_
 POSIX API returns CPU ticks, so we need to divide by CPU speed. More...
 
TimeDiff_ accumulated_times_
 
TimeDiff_ last_start_
 point in time of last start() More...
 
bool is_running_ = false
 state of stop watch, either true(on) or false(off) More...
 
bool isRunning () const
 
bool operator== (const StopWatch &stop_watch) const
 
bool operator!= (const StopWatch &stop_watch) const
 
bool operator< (const StopWatch &stop_watch) const
 
bool operator<= (const StopWatch &stop_watch) const
 
bool operator>= (const StopWatch &stop_watch) const
 
bool operator> (const StopWatch &stop_watch) const
 
String toString () const
 get a compact representation of the current time status. More...
 
static String toString (const double time_in_seconds)
 
TimeDiff_ snapShot_ () const
 get the absolute times for current system, user and kernel times More...
 

Detailed Description

This class is used to determine the current process' CPU (user and/or kernel) and wall time.

CPU time is measured as sum of all threads of the current process.

To read a time, the stopwatch must be started beforehand, but not necessarily stopped.

You can stop() the timer and resume() it later to omit intermediate steps which should not count towards the measured times.

Member Typedef Documentation

◆ TimeType

typedef clock_t TimeType
private

Member Function Documentation

◆ clear()

void clear ( )

Clear and stop the stop watch. This sets the stop watch to zero and stops it when running.

See also
reset

◆ getClockTime()

double getClockTime ( ) const

Get clock time. Return the accumulated wall clock (real) time in seconds.

Referenced by main().

◆ getCPUTime()

double getCPUTime ( ) const

Get CPU time. Return the accumulated CPU time in seconds. CPU time is the sum of user time and system time across all threads.

◆ getSystemTime()

double getSystemTime ( ) const

Get user time. Return the accumulated system time in seconds across all threads.

◆ getUserTime()

double getUserTime ( ) const

Get user time. Return the accumulated user time in seconds across all threads.

◆ isRunning()

bool isRunning ( ) const

Return true if the stop watch is running.

Returns
bool true if the stop watch is running, false otherwise

◆ operator!=()

bool operator!= ( const StopWatch stop_watch) const

Inequality operator. Return false if two stop watches differ in any way, i.e. they differ in either the clock, user, or system time or have a different running status.

Parameters
stop_watchthe stop watch to compare with
Returns
bool true on inequality, false otherwise

◆ operator<()

bool operator< ( const StopWatch stop_watch) const

Lesser than operator. Return true, if the stop watch is in all timings lesser than the stop watch to be compared with (clock, user and system time).

Parameters
stop_watchthe stop watch to compare with
Returns
bool true if all times are lesser

◆ operator<=()

bool operator<= ( const StopWatch stop_watch) const

Lesser or equal operator. Return true, if the stop watch is in all timings lesser or equal than the stop watch to be compared with (clock, user and system time).

Parameters
stop_watchthe stop watch to compare with
Returns
bool true if all times are lesser or equal

◆ operator==()

bool operator== ( const StopWatch stop_watch) const

Equality operator. Return true if two stop watches are equal, i.e. they contain exactly the same time intervals for clock, user and system time and have the same running status.

Parameters
stop_watchthe stop watch to compare with
Returns
bool true on equality, false otherwise

◆ operator>()

bool operator> ( const StopWatch stop_watch) const

Greater operator. Return true, if the stop watch is in all timings greater than the stop watch to be compared with (clock, user and system time).

Parameters
stop_watchthe stop watch to compare with
Returns
bool true if all times are greater

◆ operator>=()

bool operator>= ( const StopWatch stop_watch) const

Greater or equal operator. Return true, if the stop watch is in all timings greater or equal than the stop watch to be compared with (clock, user and system time).

Parameters
stop_watchthe stop watch to compare with
Returns
bool true if all times are greater or equal

◆ reset()

void reset ( )

Clear the stop watch but keep running.

The stop watch is reset to 0, but not stopped (if running).

See also
clear

◆ resume()

void resume ( )

Resume a stopped StopWatch.

Exceptions
Exception::Preconditionif the StopWatch is already running

◆ snapShot_()

TimeDiff_ snapShot_ ( ) const
private

get the absolute times for current system, user and kernel times

◆ start()

void start ( )

Start the stop watch.

Starting, Stopping and Resetting the stop watch

If the watch holds data from previous measurements, these will be reset before starting up, i.e. it is not possible to resume by start(), stop(), start(). Use resume(), stop(), resume() instead.

Exceptions
Exception::Preconditionif the StopWatch is already running

Referenced by main().

◆ stop()

void stop ( )

Stop the stop watch (can be resumed later). If the stop watch was not running an exception is thrown.

Exceptions
Exception::Preconditionif the StopWatch is not running

Referenced by main().

◆ toString() [1/2]

String toString ( ) const

get a compact representation of the current time status.

The output will be something like: 2.10 s (wall), 1.67 s (CPU), 0.12 s (system), 1.54 s (user)

◆ toString() [2/2]

static String toString ( const double  time_in_seconds)
static

custom string formatting of time, using only the minimal number of units required (e.g., does not report hours when seconds suffice).

Member Data Documentation

◆ accumulated_times_

TimeDiff_ accumulated_times_
private

currently accumulated times between start to stop intervals (initially 0), not counting the currently running interval which started at last_start_

◆ cpu_speed_

const PointerSizeInt cpu_speed_
staticprivate

POSIX API returns CPU ticks, so we need to divide by CPU speed.

◆ is_running_

bool is_running_ = false
private

state of stop watch, either true(on) or false(off)

◆ last_start_

TimeDiff_ last_start_
private

point in time of last start()