LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
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...
 

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...
 
template<typename Unit >
testing::StopWatch< DefaultUnit, Clock >::ElapsedTime_t elapsed () const
 
template<typename Unit >
testing::StopWatch< DefaultUnit, Clock >::ElapsedTime_t partial () const
 
template<typename Unit >
testing::StopWatch< DefaultUnit, Clock >::ElapsedTime_t previous () const
 
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...
 
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...
 

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...
 

Protected Member Functions

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...
 

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"
<< std::endl;

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

Requirements

On DefaultUnit type:

  • must be a std::chrono::duration specialisation

On Clock type:

  • must have a now() static function returning std::chrono::time_point

Definition at line 75 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 80 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 81 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 84 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 157 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 139 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 187 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::isRunning, testing::StopWatch< DefaultUnit, Clock >::now(), and testing::StopWatch< DefaultUnit, Clock >::previousTime.

188  : lastStart{start ? now() : TimePoint_t{}}, previousTime{}, isRunning{start}
189 {}
decltype(Clock_t::now()) TimePoint_t
type to store start time
Definition: StopWatch.h:139
TimePoint_t lastStart
time of the last start
Definition: StopWatch.h:141
bool isRunning
whether we are measuring time now
Definition: StopWatch.h:143
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:142
static TimePoint_t now()
Returns the current time point from our clock.
Definition: StopWatch.h:271
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 193 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::previousTime.

194  : StopWatch(start)
195 {
196  previousTime = prev;
197 }
StopWatch(bool start=true)
Initializes and starts the timer.
Definition: StopWatch.h:187
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:142

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 306 of file StopWatch.h.

307 {
308  return std::chrono::duration_cast<makeDuration_t<Unit>>(dur);
309 }
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 = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit >
testing::StopWatch<DefaultUnit, Clock>::ElapsedTime_t testing::StopWatch< DefaultUnit, Clock >::elapsed ( ) const

Definition at line 237 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::running().

238 {
239  auto const prev = previous<Unit>();
240  return running() ? (prev + partial<Unit>()) : prev;
241 } // testing::StopWatch<>::elapsed()
bool running() const
Returns whether the watch is tracking time right now.
Definition: StopWatch.h:263
template<typename DefaultUnit , typename Clock >
testing::StopWatch< DefaultUnit, Clock >::TimePoint_t testing::StopWatch< DefaultUnit, Clock >::now ( )
staticprotected
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 = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit >
testing::StopWatch<DefaultUnit, Clock>::ElapsedTime_t testing::StopWatch< DefaultUnit, Clock >::partial ( ) const

Definition at line 247 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::partialDur(), and testing::StopWatch< DefaultUnit, Clock >::running().

248 {
249  return running() ? durationTo<Unit>(partialDur()).count() : ElapsedTime_t(0);
250 } // testing::StopWatch<>::partial()
bool running() const
Returns whether the watch is tracking time right now.
Definition: StopWatch.h:263
DefaultUnit_t partialDur() const
Returns partial time as a duration.
Definition: StopWatch.h:279
typename DefaultUnit_t::rep ElapsedTime_t
Type representing the reported time.
Definition: StopWatch.h:84
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 279 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::lastStart, and testing::StopWatch< DefaultUnit, Clock >::now().

Referenced by testing::StopWatch< DefaultUnit, Clock >::partial(), and testing::StopWatch< DefaultUnit, Clock >::stop().

280 {
281  return std::chrono::duration_cast<DefaultUnit_t>(now() - lastStart);
282 } // testing::StopWatch<>::setPrevious()
DefaultUnit DefaultUnit_t
default unit for time report
Definition: StopWatch.h:81
TimePoint_t lastStart
time of the last start
Definition: StopWatch.h:141
static TimePoint_t now()
Returns the current time point from our clock.
Definition: StopWatch.h:271
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 = std::chrono::duration<double>, typename Clock = std::chrono::high_resolution_clock>
template<typename Unit >
testing::StopWatch<DefaultUnit, Clock>::ElapsedTime_t testing::StopWatch< DefaultUnit, Clock >::previous ( ) const

Definition at line 256 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::previousTime.

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

Restarts the watch; previous time is forgotten.

Definition at line 201 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::isRunning, testing::StopWatch< DefaultUnit, Clock >::lastStart, testing::StopWatch< DefaultUnit, Clock >::now(), and testing::StopWatch< DefaultUnit, Clock >::previousTime.

202 {
203  lastStart = now();
204  isRunning = true;
206 } // testing::StopWatch<>::restart()
DefaultUnit DefaultUnit_t
default unit for time report
Definition: StopWatch.h:81
TimePoint_t lastStart
time of the last start
Definition: StopWatch.h:141
bool isRunning
whether we are measuring time now
Definition: StopWatch.h:143
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:142
static TimePoint_t now()
Returns the current time point from our clock.
Definition: StopWatch.h:271
template<typename DefaultUnit , typename Clock >
void testing::StopWatch< DefaultUnit, Clock >::resume ( )

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

Definition at line 210 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::isRunning, testing::StopWatch< DefaultUnit, Clock >::lastStart, testing::StopWatch< DefaultUnit, Clock >::now(), and testing::StopWatch< DefaultUnit, Clock >::running().

211 {
212  if (running()) return;
213  lastStart = now();
214  isRunning = true;
215 } // testing::StopWatch<>::resume()
bool running() const
Returns whether the watch is tracking time right now.
Definition: StopWatch.h:263
TimePoint_t lastStart
time of the last start
Definition: StopWatch.h:141
bool isRunning
whether we are measuring time now
Definition: StopWatch.h:143
static TimePoint_t now()
Returns the current time point from our clock.
Definition: StopWatch.h:271
template<typename DefaultUnit , typename Clock >
bool testing::StopWatch< DefaultUnit, Clock >::running ( ) const

Returns whether the watch is tracking time right now.

Definition at line 263 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::isRunning.

Referenced by testing::StopWatch< DefaultUnit, Clock >::elapsed(), testing::StopWatch< DefaultUnit, Clock >::partial(), and testing::StopWatch< DefaultUnit, Clock >::resume().

264 {
265  return isRunning;
266 }
bool isRunning
whether we are measuring time now
Definition: StopWatch.h:143
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 228 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::previousTime.

229 {
230  previousTime = std::chrono::duration_cast<DefaultUnit_t>(dur);
231 } // testing::StopWatch<>::setPrevious()
DefaultUnit DefaultUnit_t
default unit for time report
Definition: StopWatch.h:81
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:142
template<typename DefaultUnit , typename Clock >
void testing::StopWatch< DefaultUnit, Clock >::stop ( )

Pauses the watch.

Definition at line 219 of file StopWatch.h.

References testing::StopWatch< DefaultUnit, Clock >::isRunning, testing::StopWatch< DefaultUnit, Clock >::partialDur(), and testing::StopWatch< DefaultUnit, Clock >::previousTime.

220 {
222  isRunning = false;
223 } // testing::StopWatch<>::stop()
DefaultUnit_t partialDur() const
Returns partial time as a duration.
Definition: StopWatch.h:279
bool isRunning
whether we are measuring time now
Definition: StopWatch.h:143
DefaultUnit_t previousTime
time accumulated from previous runs
Definition: StopWatch.h:142

Member Data Documentation

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

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