Loading [MathJax]/extensions/tex2jax.js

Dune Core Modules (unstable)

timer.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5#ifndef DUNE_TIMER_HH
6#define DUNE_TIMER_HH
7
8#include <chrono>
9
10namespace Dune {
11
30 class Timer
31 {
32 using Clock = std::chrono::high_resolution_clock;
33 using Units = std::chrono::duration<double>; // seconds stored as double
34 public:
35
40 Timer (bool startImmediately=true) noexcept
41 {
42 isRunning_ = startImmediately;
43 reset();
44 }
45
47 void reset() noexcept
48 {
49 sumElapsed_ = std::chrono::seconds{0};
50 storedLastElapsed_ = std::chrono::seconds{0};
51 cstart = Clock::now();
52 }
53
54
56 void start() noexcept
57 {
58 if (not (isRunning_))
59 {
60 cstart = Clock::now();
61 isRunning_ = true;
62 }
63 }
64
65
67 double elapsed () const noexcept
68 {
69 return durationCast(rawElapsed());
70 }
71
73 double lastElapsed () const noexcept
74 {
75 return durationCast(rawLastElapsed());
76 }
77
79 double stop() noexcept
80 {
81 if (isRunning_)
82 {
83 // update storedLastElapsed_ and sumElapsed_ and stop timer
84 storedLastElapsed_ = rawLastElapsed();
85 sumElapsed_ += storedLastElapsed_;
86 isRunning_ = false;
87 }
88 return elapsed();
89 }
90
91
92 private:
93
94 bool isRunning_;
95 Clock::duration sumElapsed_;
96 Clock::duration storedLastElapsed_;
97
98 Clock::duration rawElapsed () const noexcept
99 {
100 // if timer is running add the time elapsed since last start to sum
101 if (isRunning_)
102 return sumElapsed_ + rawLastElapsed();
103
104 return sumElapsed_;
105 }
106
108 Clock::duration rawLastElapsed () const noexcept
109 {
110 // if timer is running return the current value
111 if (isRunning_)
112 return Clock::now() - cstart;
113
114 // if timer is not running return stored value from last run
115 return storedLastElapsed_;
116 }
117
118 double durationCast(Clock::duration duration) const noexcept {
119 return std::chrono::duration_cast<Units>(duration).count();
120 }
121
122 Clock::time_point cstart;
123 }; // end class Timer
124
127} // end namespace
128
129#endif
A simple stop watch.
Definition: timer.hh:31
void reset() noexcept
Reset timer while keeping the running/stopped state.
Definition: timer.hh:47
double stop() noexcept
Stop the timer and return elapsed().
Definition: timer.hh:79
Timer(bool startImmediately=true) noexcept
A new timer, create and reset.
Definition: timer.hh:40
double elapsed() const noexcept
Get elapsed user-time from last reset until now/last stop in seconds.
Definition: timer.hh:67
double lastElapsed() const noexcept
Get elapsed user-time from last start until now/last stop in seconds.
Definition: timer.hh:73
void start() noexcept
Start the timer and continue measurement if it is not running. Otherwise do nothing.
Definition: timer.hh:56
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden & Uni Heidelberg  |  generated with Hugo v0.111.3 (Mar 12, 23:28, 2025)