Dune Core Modules (2.6.0)

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