12 #ifndef LARCORE_TESTUTILS_STOPWATCH_H
13 #define LARCORE_TESTUTILS_STOPWATCH_H
19 #include <type_traits>
25 template <
typename Duration>
76 typename DefaultUnit = std::chrono::duration<double>,
77 typename Clock = std::chrono::high_resolution_clock
81 "DefaultUnit type is not a std::chrono::duration specialization");
101 template <
typename Unit>
118 template <
typename Unit = DefaultUnit_t>
127 template <
typename Unit = DefaultUnit_t>
131 template <
typename Unit = DefaultUnit_t>
135 template <
typename Unit = DefaultUnit_t>
163 template <
typename Unit>
167 template <
typename Unit,
typename From>
182 template <
typename Duration>
183 struct isDuration:
public std::false_type {};
185 template <
typename Rep,
typename Period>
187 :
public std::true_type
198 template <
typename DefaultUnit,
typename Clock>
206 template <
typename DefaultUnit,
typename Clock>
207 template <
typename Unit>
209 (Unit prev,
bool start )
217 template <
typename DefaultUnit,
typename Clock>
226 template <
typename DefaultUnit,
typename Clock>
228 if (running())
return;
235 template <
typename DefaultUnit,
typename Clock>
237 previousTime += partialDur();
243 template <
typename DefaultUnit,
typename Clock>
244 template <
typename Unit>
246 previousTime = std::chrono::duration_cast<
DefaultUnit_t>(dur);
251 template <
typename DefaultUnit,
typename Clock>
252 template <
typename Unit>
255 auto const prev = previous<Unit>();
256 return running()? (prev + partial<Unit>()): prev;
261 template <
typename DefaultUnit,
typename Clock>
262 template <
typename Unit>
270 template <
typename DefaultUnit,
typename Clock>
271 template <
typename Unit>
274 return durationTo<Unit>(previousTime).
count();
279 template <
typename DefaultUnit,
typename Clock>
281 {
return isRunning; }
285 template <
typename DefaultUnit,
typename Clock>
292 template <
typename DefaultUnit,
typename Clock>
302 template <
typename DefaultUnit,
typename Clock>
303 template <
typename Rep,
typename Duration>
305 <std::chrono::duration<Rep, Duration>>
307 using type = std::chrono::duration<Rep, Duration>;
311 template <
typename DefaultUnit,
typename Clock>
312 template <std::
intmax_t Num, std::
intmax_t Den>
315 using type = std::chrono::duration<
324 template <
typename DefaultUnit,
typename Clock>
325 template <
typename Unit,
typename From>
333 #endif // LARCORE_TESTUTILS_STOPWATCH_H
bool running() const
Returns whether the watch is tracking time right now.
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
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
std::size_t count(Cont const &cont)
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.