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 #ifndef TIMER_USE_STD_CLOCK
9 // headers for std::chrono
10 #include <chrono>
11 #else
12 // headers for std::clock
13 #include <ctime>
14 #endif
15 
16 namespace Dune {
17 
42  class Timer
43  {
44  public:
45 
50  Timer (bool startImmediately=true) noexcept
51  {
52  isRunning_ = startImmediately;
53  reset();
54  }
55 
57  void reset() noexcept
58  {
59  sumElapsed_ = 0.0;
60  storedLastElapsed_ = 0.0;
61  rawReset();
62  }
63 
64 
66  void start() noexcept
67  {
68  if (not (isRunning_))
69  {
70  rawReset();
71  isRunning_ = true;
72  }
73  }
74 
75 
77  double elapsed () const noexcept
78  {
79  // if timer is running add the time elapsed since last start to sum
80  if (isRunning_)
81  return sumElapsed_ + lastElapsed();
82 
83  return sumElapsed_;
84  }
85 
86 
88  double lastElapsed () const noexcept
89  {
90  // if timer is running return the current value
91  if (isRunning_)
92  return rawElapsed();
93 
94  // if timer is not running return stored value from last run
95  return storedLastElapsed_;
96  }
97 
98 
100  double stop() noexcept
101  {
102  if (isRunning_)
103  {
104  // update storedLastElapsed_ and sumElapsed_ and stop timer
105  storedLastElapsed_ = lastElapsed();
106  sumElapsed_ += storedLastElapsed_;
107  isRunning_ = false;
108  }
109  return elapsed();
110  }
111 
112 
113  private:
114 
115  bool isRunning_;
116  double sumElapsed_;
117  double storedLastElapsed_;
118 
119 
120 #ifdef TIMER_USE_STD_CLOCK
121  void rawReset() noexcept
122  {
123  cstart = std::clock();
124  }
125 
126  double rawElapsed () const noexcept
127  {
128  return (std::clock()-cstart) / static_cast<double>(CLOCKS_PER_SEC);
129  }
130 
131  std::clock_t cstart;
132 #else
133  void rawReset() noexcept
134  {
135  cstart = std::chrono::high_resolution_clock::now();
136  }
137 
138  double rawElapsed () const noexcept
139  {
140  std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
141  std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double> >(now - cstart);
142  return time_span.count();
143  }
144 
145  std::chrono::high_resolution_clock::time_point cstart;
146 #endif
147  }; // end class Timer
148 
151 } // end namespace
152 
153 #endif
A simple stop watch.
Definition: timer.hh:43
void reset() noexcept
Reset timer while keeping the running/stopped state.
Definition: timer.hh:57
double stop() noexcept
Stop the timer and return elapsed().
Definition: timer.hh:100
Timer(bool startImmediately=true) noexcept
A new timer, create and reset.
Definition: timer.hh:50
double elapsed() const noexcept
Get elapsed user-time from last reset until now/last stop in seconds.
Definition: timer.hh:77
double lastElapsed() const noexcept
Get elapsed user-time from last start until now/last stop in seconds.
Definition: timer.hh:88
void start() noexcept
Start the timer and continue measurement if it is not running. Otherwise do nothing.
Definition: timer.hh:66
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (Apr 26, 22:29, 2024)