Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
testing::StopWatch< DefaultUnit, Clock > Class Template Reference

Provides time interval measurements. More...

#include <StopWatch.h>

Classes

struct  makeDurationTrait
 Trait whose type member is a std::chrono::duration type. More...
 
struct  makeDurationTrait< std::chrono::duration< Rep, Duration > >
 
struct  makeDurationTrait< std::ratio< Num, Den > >
 

Public Types

using Clock_t = Clock
 type of clock used to extract current time More...
 
using DefaultUnit_t = DefaultUnit
 default unit for time report More...
 
using ElapsedTime_t = typename DefaultUnit_t::rep
 Type representing the reported time. More...
 
using Clock_t = Clock
 type of clock used to extract current time More...
 
using DefaultUnit_t = DefaultUnit
 default unit for time report More...
 
using ElapsedTime_t = typename DefaultUnit_t::rep
 Type representing the reported time. More...
 

Public Member Functions

 StopWatch (bool start=true)
 Initializes and starts the timer. More...
 
template<typename Unit >
 StopWatch (Unit prev, bool start=true)
 Initializes and starts the timer. More...
 
 StopWatch (bool start=true)
 Initializes and starts the timer. More...
 
template<typename Unit >
 StopWatch (Unit prev, bool start=true)
 Initializes and starts the timer. More...
 
Watch control
void restart ()
 Restarts the watch; previous time is forgotten. More...
 
void resume ()
 Resumes the run of the watch; previous time is preserved. More...
 
void stop ()
 Pauses the watch. More...
 
template<typename Unit = DefaultUnit_t>
void setPrevious (Unit dur)
 Changes the amount of time accumulated before this run. More...
 
void restart ()
 Restarts the watch; previous time is forgotten. More...
 
void resume ()
 Resumes the run of the watch; previous time is preserved. More...
 
void stop ()
 Pauses the watch. More...
 
template<typename Unit = DefaultUnit_t>
void setPrevious (Unit dur)
 Changes the amount of time accumulated before this run. More...
 
Query
template<typename Unit = DefaultUnit_t>
ElapsedTime_t elapsed () const
 Returns the total time spent running since the last restart. More...
 
template<typename Unit = DefaultUnit_t>
ElapsedTime_t partial () const
 Returns the time spent running since the last resume. More...
 
template<typename Unit = DefaultUnit_t>
ElapsedTime_t previous () const
 Returns the time accumulated before the current run. More...
 
bool running () const
 Returns whether the watch is tracking time right now. More...
 
template<typename Unit = DefaultUnit_t>
ElapsedTime_t elapsed () const
 Returns the total time spent running since the last restart. More...
 
template<typename Unit = DefaultUnit_t>
ElapsedTime_t partial () const
 Returns the time spent running since the last resume. More...
 
template<typename Unit = DefaultUnit_t>
ElapsedTime_t previous () const
 Returns the time accumulated before the current run. More...
 
bool running () const
 Returns whether the watch is tracking time right now. More...
 

Protected Types

using TimePoint_t = decltype(Clock_t::now())
 type to store start time More...
 
template<typename Unit >
using makeDuration_t = typename makeDurationTrait< Unit >::type
 Type of std::chrono::duration type constructed from makeDurationTrait. More...
 
using TimePoint_t = decltype(Clock_t::now())
 type to store start time More...
 
template<typename Unit >
using makeDuration_t = typename makeDurationTrait< Unit >::type
 Type of std::chrono::duration type constructed from makeDurationTrait. More...
 

Protected Member Functions

DefaultUnit_t partialDur () const
 Returns partial time as a duration. More...
 
DefaultUnit_t partialDur () const
 Returns partial time as a duration. More...
 

Static Protected Member Functions

static TimePoint_t now ()
 Returns the current time point from our clock. More...
 
template<typename Unit , typename From >
static auto durationTo (From const &dur)
 Convert a duration into a unit (may be a ratio or a duration) More...
 
static TimePoint_t now ()
 Returns the current time point from our clock. More...
 
template<typename Unit , typename From >
static auto durationTo (From const &dur)
 Convert a duration into a unit (may be a ratio or a duration) More...
 

Protected Attributes

TimePoint_t lastStart
 time of the last start More...
 
DefaultUnit_t previousTime
 time accumulated from previous runs More...
 
bool isRunning
 whether we are measuring time now More...
 

Detailed Description

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
class testing::StopWatch< DefaultUnit, Clock >

Provides time interval measurements.

Template Parameters
DefaultUnitunit reported by default (seconds, floating point)
Clocktype of clock object used (default: high_resolution_clock)

The stopwatch keeps track of the clock and can return the time elapsed from a previous time mark. Example of use:

// do initialisation of task A
testing::StopWatch<> timer; // starts automatically
// execute task A
timer.stop();
// do initialisation of task B
timer.resume()
// execute task B
timer.stop()
std::cout << "Tasks A and B took " << timer.elapsed() << " seconds"

The time from all methods returning a value are in the DefaultUnit_t unit.

Requirements

On DefaultUnit type:

On Clock type:

Definition at line 79 of file StopWatch.h.

Member Typedef Documentation

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
using testing::StopWatch< DefaultUnit, Clock >::Clock_t = Clock

type of clock used to extract current time

Definition at line 84 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
using testing::StopWatch< DefaultUnit, Clock >::Clock_t = Clock

type of clock used to extract current time

Definition at line 84 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
using testing::StopWatch< DefaultUnit, Clock >::DefaultUnit_t = DefaultUnit

default unit for time report

Definition at line 85 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
using testing::StopWatch< DefaultUnit, Clock >::DefaultUnit_t = DefaultUnit

default unit for time report

Definition at line 85 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
using testing::StopWatch< DefaultUnit, Clock >::ElapsedTime_t = typename DefaultUnit_t::rep

Type representing the reported time.

Definition at line 88 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
using testing::StopWatch< DefaultUnit, Clock >::ElapsedTime_t = typename DefaultUnit_t::rep

Type representing the reported time.

Definition at line 88 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit >
using testing::StopWatch< DefaultUnit, Clock >::makeDuration_t = typename makeDurationTrait<Unit>::type
protected

Type of std::chrono::duration type constructed from makeDurationTrait.

Definition at line 164 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit >
using testing::StopWatch< DefaultUnit, Clock >::makeDuration_t = typename makeDurationTrait<Unit>::type
protected

Type of std::chrono::duration type constructed from makeDurationTrait.

Definition at line 164 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
using testing::StopWatch< DefaultUnit, Clock >::TimePoint_t = decltype(Clock_t::now())
protected

type to store start time

Definition at line 145 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
using testing::StopWatch< DefaultUnit, Clock >::TimePoint_t = decltype(Clock_t::now())
protected

type to store start time

Definition at line 145 of file StopWatch.h.

Constructor & Destructor Documentation

template<typename DefaultUnit , typename Clock >
testing::StopWatch< DefaultUnit, Clock >::StopWatch ( bool  start = true)

Initializes and starts the timer.

Parameters
startwhether to start immediately (default: true)

Definition at line 199 of file StopWatch.h.

200  : lastStart{start? now(): TimePoint_t{}}
201  , previousTime{}
202  , isRunning{start}
203 {}
decltype(Clock_t::now()) TimePoint_t
type to store start time
Definition: StopWatch.h:145
TimePoint_t lastStart
time of the last start
Definition: StopWatch.h:147
bool isRunning
whether we are measuring time now
Definition: StopWatch.h:149
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:148
static TimePoint_t now()
Returns the current time point from our clock.
Definition: StopWatch.h:287
template<typename DefaultUnit , typename Clock >
template<typename Unit >
testing::StopWatch< DefaultUnit, Clock >::StopWatch ( Unit  prev,
bool  start = true 
)

Initializes and starts the timer.

Parameters
prevtime already accumulated on start
startwhether to start immediately (default: true)

Definition at line 209 of file StopWatch.h.

210  : StopWatch(start)
211 {
212  previousTime = prev;
213 }
StopWatch(bool start=true)
Initializes and starts the timer.
Definition: StopWatch.h:199
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:148
template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
testing::StopWatch< DefaultUnit, Clock >::StopWatch ( bool  start = true)

Initializes and starts the timer.

Parameters
startwhether to start immediately (default: true)
template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit >
testing::StopWatch< DefaultUnit, Clock >::StopWatch ( Unit  prev,
bool  start = true 
)

Initializes and starts the timer.

Parameters
prevtime already accumulated on start
startwhether to start immediately (default: true)

Member Function Documentation

template<typename DefaultUnit , typename Clock >
template<typename Unit , typename From >
auto testing::StopWatch< DefaultUnit, Clock >::durationTo ( From const &  dur)
staticprotected

Convert a duration into a unit (may be a ratio or a duration)

Definition at line 326 of file StopWatch.h.

327  { return std::chrono::duration_cast<makeDuration_t<Unit>>(dur); }
template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit , typename From >
static auto testing::StopWatch< DefaultUnit, Clock >::durationTo ( From const &  dur)
staticprotected

Convert a duration into a unit (may be a ratio or a duration)

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit = DefaultUnit_t>
ElapsedTime_t testing::StopWatch< DefaultUnit, Clock >::elapsed ( ) const

Returns the total time spent running since the last restart.

template<typename DefaultUnit , typename Clock >
template<typename Unit >
testing::StopWatch< DefaultUnit, Clock >::ElapsedTime_t testing::StopWatch< DefaultUnit, Clock >::elapsed ( ) const

Returns the total time spent running since the last restart.

Definition at line 254 of file StopWatch.h.

254  {
255  auto const prev = previous<Unit>();
256  return running()? (prev + partial<Unit>()): prev;
257 } // testing::StopWatch<>::elapsed()
bool running() const
Returns whether the watch is tracking time right now.
Definition: StopWatch.h:280
template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
static TimePoint_t testing::StopWatch< DefaultUnit, Clock >::now ( )
staticprotected

Returns the current time point from our clock.

template<typename DefaultUnit , typename Clock >
testing::StopWatch< DefaultUnit, Clock >::TimePoint_t testing::StopWatch< DefaultUnit, Clock >::now ( )
staticprotected

Returns the current time point from our clock.

Definition at line 287 of file StopWatch.h.

288  { return Clock_t::now(); }
template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit = DefaultUnit_t>
ElapsedTime_t testing::StopWatch< DefaultUnit, Clock >::partial ( ) const

Returns the time spent running since the last resume.

template<typename DefaultUnit , typename Clock >
template<typename Unit >
testing::StopWatch< DefaultUnit, Clock >::ElapsedTime_t testing::StopWatch< DefaultUnit, Clock >::partial ( ) const

Returns the time spent running since the last resume.

Definition at line 264 of file StopWatch.h.

264  {
265  return running()? durationTo<Unit>(partialDur()).count(): ElapsedTime_t(0);
266 } // testing::StopWatch<>::partial()
bool running() const
Returns whether the watch is tracking time right now.
Definition: StopWatch.h:280
DefaultUnit_t partialDur() const
Returns partial time as a duration.
Definition: StopWatch.h:294
typename DefaultUnit_t::rep ElapsedTime_t
Type representing the reported time.
Definition: StopWatch.h:88
template<typename DefaultUnit , typename Clock >
testing::StopWatch< DefaultUnit, Clock >::DefaultUnit_t testing::StopWatch< DefaultUnit, Clock >::partialDur ( ) const
protected

Returns partial time as a duration.

Definition at line 294 of file StopWatch.h.

294  {
295  return std::chrono::duration_cast<DefaultUnit_t>(now() - lastStart);
296 } // testing::StopWatch<>::setPrevious()
DefaultUnit DefaultUnit_t
default unit for time report
Definition: StopWatch.h:85
TimePoint_t lastStart
time of the last start
Definition: StopWatch.h:147
static TimePoint_t now()
Returns the current time point from our clock.
Definition: StopWatch.h:287
template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
DefaultUnit_t testing::StopWatch< DefaultUnit, Clock >::partialDur ( ) const
protected

Returns partial time as a duration.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit = DefaultUnit_t>
ElapsedTime_t testing::StopWatch< DefaultUnit, Clock >::previous ( ) const

Returns the time accumulated before the current run.

template<typename DefaultUnit , typename Clock >
template<typename Unit >
testing::StopWatch< DefaultUnit, Clock >::ElapsedTime_t testing::StopWatch< DefaultUnit, Clock >::previous ( ) const

Returns the time accumulated before the current run.

Definition at line 273 of file StopWatch.h.

273  {
274  return durationTo<Unit>(previousTime).count();
275 } // testing::StopWatch<>::previous()
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:148
template<typename DefaultUnit , typename Clock >
void testing::StopWatch< DefaultUnit, Clock >::restart ( )

Restarts the watch; previous time is forgotten.

Definition at line 218 of file StopWatch.h.

218  {
219  lastStart = now();
220  isRunning = true;
222 } // testing::StopWatch<>::restart()
DefaultUnit DefaultUnit_t
default unit for time report
Definition: StopWatch.h:85
TimePoint_t lastStart
time of the last start
Definition: StopWatch.h:147
bool isRunning
whether we are measuring time now
Definition: StopWatch.h:149
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:148
static TimePoint_t now()
Returns the current time point from our clock.
Definition: StopWatch.h:287
template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
void testing::StopWatch< DefaultUnit, Clock >::restart ( )

Restarts the watch; previous time is forgotten.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
void testing::StopWatch< DefaultUnit, Clock >::resume ( )

Resumes the run of the watch; previous time is preserved.

template<typename DefaultUnit , typename Clock >
void testing::StopWatch< DefaultUnit, Clock >::resume ( )

Resumes the run of the watch; previous time is preserved.

Definition at line 227 of file StopWatch.h.

227  {
228  if (running()) return;
229  lastStart = now();
230  isRunning = true;
231 } // testing::StopWatch<>::resume()
bool running() const
Returns whether the watch is tracking time right now.
Definition: StopWatch.h:280
TimePoint_t lastStart
time of the last start
Definition: StopWatch.h:147
bool isRunning
whether we are measuring time now
Definition: StopWatch.h:149
static TimePoint_t now()
Returns the current time point from our clock.
Definition: StopWatch.h:287
template<typename DefaultUnit , typename Clock >
bool testing::StopWatch< DefaultUnit, Clock >::running ( ) const

Returns whether the watch is tracking time right now.

Definition at line 280 of file StopWatch.h.

281  { return isRunning; }
bool isRunning
whether we are measuring time now
Definition: StopWatch.h:149
template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
bool testing::StopWatch< DefaultUnit, Clock >::running ( ) const

Returns whether the watch is tracking time right now.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit = DefaultUnit_t>
void testing::StopWatch< DefaultUnit, Clock >::setPrevious ( Unit  dur)

Changes the amount of time accumulated before this run.

template<typename DefaultUnit , typename Clock >
template<typename Unit >
void testing::StopWatch< DefaultUnit, Clock >::setPrevious ( Unit  dur)

Changes the amount of time accumulated before this run.

Definition at line 245 of file StopWatch.h.

245  {
246  previousTime = std::chrono::duration_cast<DefaultUnit_t>(dur);
247 } // testing::StopWatch<>::setPrevious()
DefaultUnit DefaultUnit_t
default unit for time report
Definition: StopWatch.h:85
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:148
template<typename DefaultUnit , typename Clock >
void testing::StopWatch< DefaultUnit, Clock >::stop ( )

Pauses the watch.

Definition at line 236 of file StopWatch.h.

236  {
238  isRunning = false;
239 } // testing::StopWatch<>::stop()
DefaultUnit_t partialDur() const
Returns partial time as a duration.
Definition: StopWatch.h:294
bool isRunning
whether we are measuring time now
Definition: StopWatch.h:149
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:148
template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
void testing::StopWatch< DefaultUnit, Clock >::stop ( )

Pauses the watch.

Member Data Documentation

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
bool testing::StopWatch< DefaultUnit, Clock >::isRunning
protected

whether we are measuring time now

Definition at line 149 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
TimePoint_t testing::StopWatch< DefaultUnit, Clock >::lastStart
protected

time of the last start

Definition at line 147 of file StopWatch.h.

template<typename DefaultUnit = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
DefaultUnit_t testing::StopWatch< DefaultUnit, Clock >::previousTime
protected

time accumulated from previous runs

Definition at line 148 of file StopWatch.h.


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