Dune Core Modules (2.4.1)

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// headers for stderror(3)
15#include <cstring>
16
17// access to errno in C++
18#include <cerrno>
19
20#include "exceptions.hh"
21
22namespace Dune {
23
33 class TimerError : public SystemError {} ;
34
35
51 class Timer
52 {
53 public:
54
59 Timer (bool startImmediately=true) throw(TimerError)
60 {
61 isRunning_ = startImmediately;
62 reset();
63 }
64
66 void reset() throw (TimerError)
67 {
68 sumElapsed_ = 0.0;
69 storedLastElapsed_ = 0.0;
70 rawReset();
71 }
72
73
75 void start() throw (TimerError)
76 {
77 if (not (isRunning_))
78 {
79 rawReset();
80 isRunning_ = true;
81 }
82 }
83
84
86 double elapsed () const throw (TimerError)
87 {
88 // if timer is running add the time elapsed since last start to sum
89 if (isRunning_)
90 return sumElapsed_ + lastElapsed();
91
92 return sumElapsed_;
93 }
94
95
97 double lastElapsed () const throw (TimerError)
98 {
99 // if timer is running return the current value
100 if (isRunning_)
101 return rawElapsed();
102
103 // if timer is not running return stored value from last run
104 return storedLastElapsed_;
105 }
106
107
109 double stop() throw (TimerError)
110 {
111 if (isRunning_)
112 {
113 // update storedLastElapsed_ and sumElapsed_ and stop timer
114 storedLastElapsed_ = lastElapsed();
115 sumElapsed_ += storedLastElapsed_;
116 isRunning_ = false;
117 }
118 return elapsed();
119 }
120
121
122 private:
123
124 bool isRunning_;
125 double sumElapsed_;
126 double storedLastElapsed_;
127
128
129#ifdef TIMER_USE_STD_CLOCK
130 void rawReset() throw (TimerError)
131 {
132 cstart = std::clock();
133 }
134
135 double rawElapsed () const throw (TimerError)
136 {
137 return (std::clock()-cstart) / static_cast<double>(CLOCKS_PER_SEC);
138 }
139
140 std::clock_t cstart;
141#else
142 void rawReset() throw (TimerError)
143 {
144 cstart = std::chrono::high_resolution_clock::now();
145 }
146
147 double rawElapsed () const throw (TimerError)
148 {
149 std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
150 std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double> >(now - cstart);
151 return time_span.count();
152 }
153
154 std::chrono::high_resolution_clock::time_point cstart;
155#endif
156 }; // end class Timer
157
160} // end namespace
161
162#endif
Default exception class for OS errors.
Definition: exceptions.hh:296
Exception thrown by the Timer class
Definition: timer.hh:33
A simple stop watch.
Definition: timer.hh:52
Timer(bool startImmediately=true)
A new timer, create and reset.
Definition: timer.hh:59
void reset()
Reset timer while keeping the running/stopped state.
Definition: timer.hh:66
double stop()
Stop the timer and return elapsed().
Definition: timer.hh:109
double elapsed() const
Get elapsed user-time from last reset until now/last stop in seconds.
Definition: timer.hh:86
double lastElapsed() const
Get elapsed user-time from last start until now/last stop in seconds.
Definition: timer.hh:97
void start()
Start the timer and continue measurement if it is not running. Otherwise do nothing.
Definition: timer.hh:75
Dune namespace.
Definition: alignment.hh:10
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)