12 #ifndef LARCORE_TESTUTILS_STOPWATCH_H 13 #define LARCORE_TESTUTILS_STOPWATCH_H 19 #include <type_traits> 24 template <
typename Duration>
73 template <
typename DefaultUnit = std::chrono::duration<
double>,
74 typename Clock = std::chrono::high_resolution_clock>
77 "DefaultUnit type is not a std::chrono::duration specialization");
97 template <
typename Unit>
113 template <
typename Unit = DefaultUnit_t>
114 void setPrevious(Unit dur);
122 template <
typename Unit = DefaultUnit_t>
126 template <
typename Unit = DefaultUnit_t>
130 template <
typename Unit = DefaultUnit_t>
134 bool running()
const;
156 template <
typename Unit>
160 template <
typename Unit,
typename From>
161 static auto durationTo(From
const& dur);
173 template <
typename Duration>
174 struct isDuration :
public std::false_type {};
176 template <
typename Rep,
typename Period>
177 struct isDuration<
std::chrono::duration<Rep, Period>> :
public std::true_type {};
186 template <
typename DefaultUnit,
typename Clock>
191 template <
typename DefaultUnit,
typename Clock>
192 template <
typename Unit>
200 template <
typename DefaultUnit,
typename Clock>
209 template <
typename DefaultUnit,
typename Clock>
218 template <
typename DefaultUnit,
typename Clock>
226 template <
typename DefaultUnit,
typename Clock>
227 template <
typename Unit>
234 template <
typename DefaultUnit,
typename Clock>
235 template <
typename Unit>
239 auto const prev = previous<Unit>();
240 return running() ? (prev + partial<Unit>()) : prev;
244 template <
typename DefaultUnit,
typename Clock>
245 template <
typename Unit>
253 template <
typename DefaultUnit,
typename Clock>
254 template <
typename Unit>
262 template <
typename DefaultUnit,
typename Clock>
269 template <
typename DefaultUnit,
typename Clock>
273 return Clock_t::now();
277 template <
typename DefaultUnit,
typename Clock>
287 template <
typename DefaultUnit,
typename Clock>
288 template <
typename Rep,
typename Duration>
290 using type = std::chrono::duration<Rep, Duration>;
294 template <
typename DefaultUnit,
typename Clock>
295 template <std::
intmax_t Num, std::
intmax_t Den>
297 using type = std::chrono::duration<typename StopWatch<DefaultUnit, Clock>::ElapsedTime_t,
298 std::ratio<Num, Den>>;
304 template <
typename DefaultUnit,
typename Clock>
305 template <
typename Unit,
typename From>
313 #endif // LARCORE_TESTUTILS_STOPWATCH_H bool running() const
Returns whether the watch is tracking time right now.
DefaultUnit DefaultUnit_t
default unit for time report
decltype(Clock_t::now()) TimePoint_t
type to store start time
Type trait containing whether Duration is std::chrono::duration.
StopWatch(bool start=true)
Initializes and starts the timer.
static auto durationTo(From const &dur)
Convert a duration into a unit (may be a ratio or a duration)
TimePoint_t lastStart
time of the last start
DefaultUnit_t partialDur() const
Returns partial time as a duration.
typename DefaultUnit_t::rep ElapsedTime_t
Type representing the reported time.
void stop()
Pauses the watch.
void restart()
Restarts the watch; previous time is forgotten.
void setPrevious(Unit dur)
Changes the amount of time accumulated before this run.
Clock Clock_t
type of clock used to extract current time
Provides time interval measurements.
bool isRunning
whether we are measuring time now
ElapsedTime_t previous() const
Returns the time accumulated before the current run.
ElapsedTime_t partial() const
Returns the time spent running since the last resume.
void resume()
Resumes the run of the watch; previous time is preserved.
DefaultUnit_t previousTime
time accumulated from previous runs
Trait whose type member is a std::chrono::duration type.
typename makeDurationTrait< Unit >::type makeDuration_t
Type of std::chrono::duration type constructed from makeDurationTrait.
static TimePoint_t now()
Returns the current time point from our clock.
ElapsedTime_t elapsed() const
Returns the total time spent running since the last restart.