| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // This file is part of Bembel, the higher order C++ boundary element library. | ||
| 2 | // | ||
| 3 | // Copyright (C) 2022 see <http://www.bembel.eu> | ||
| 4 | // | ||
| 5 | // It was written as part of a cooperation of J. Doelz, H. Harbrecht, S. Kurz, | ||
| 6 | // M. Multerer, S. Schoeps, and F. Wolf at Technische Universitaet Darmstadt, | ||
| 7 | // Universitaet Basel, and Universita della Svizzera italiana, Lugano. This | ||
| 8 | // source code is subject to the GNU General Public License version 3 and | ||
| 9 | // provided WITHOUT ANY WARRANTY, see <http://www.bembel.eu> for further | ||
| 10 | // information. | ||
| 11 | #ifndef BEMBEL_SRC_IO_STOPWATCH_HPP_ | ||
| 12 | #define BEMBEL_SRC_IO_STOPWATCH_HPP_ | ||
| 13 | |||
| 14 | namespace Bembel { | ||
| 15 | |||
| 16 | namespace IO { | ||
| 17 | /** | ||
| 18 | * \ingroup IO | ||
| 19 | * \brief A simple class for benchmarking. | ||
| 20 | * | ||
| 21 | * A simple stopwatch class for benchmarking. start() starts the timer, | ||
| 22 | * lap() gives the time from either start or the last lap, stop() gives the | ||
| 23 | * overall time since start, and get_data() returns a std::vector<double> with | ||
| 24 | * all lap-times. It is written such that its own timekeeping is not measured. | ||
| 25 | */ | ||
| 26 | |||
| 27 | class Stopwatch { | ||
| 28 | std::chrono::time_point<std::chrono::high_resolution_clock> p_; | ||
| 29 | double sum_; | ||
| 30 | std::vector<double> durations_; | ||
| 31 | bool has_been_started_; | ||
| 32 | bool has_lapped_; | ||
| 33 | |||
| 34 | public: | ||
| 35 | 6 | inline Stopwatch() { | |
| 36 | 6 | has_been_started_ = false; | |
| 37 | 6 | has_lapped_ = false; | |
| 38 | 6 | } | |
| 39 | |||
| 40 | 7 | void tic(void) { p_ = std::chrono::high_resolution_clock::now(); } | |
| 41 | 6 | double toc(void) { | |
| 42 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | double dtime = std::chrono::duration<double, std::ratio<1, 1>>( |
| 43 |
1/2✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | std::chrono::high_resolution_clock::now() - p_) |
| 44 | 6 | .count(); | |
| 45 | 6 | return dtime; | |
| 46 | } | ||
| 47 | inline void start() { | ||
| 48 | durations_ = {}; | ||
| 49 | assert(has_been_started_ == false && "Stopwatch already running!"); | ||
| 50 | has_been_started_ = true; | ||
| 51 | p_ = std::chrono::high_resolution_clock::now(); | ||
| 52 | } | ||
| 53 | inline double lap() { | ||
| 54 | double out = std::chrono::duration<double, std::ratio<1, 1>>( | ||
| 55 | std::chrono::high_resolution_clock::now() - p_) | ||
| 56 | .count(); | ||
| 57 | durations_.push_back(out); | ||
| 58 | assert(has_been_started_ == true && "Stopwatch must be started first!"); | ||
| 59 | has_lapped_ = true; | ||
| 60 | p_ = std::chrono::high_resolution_clock::now(); | ||
| 61 | return out; | ||
| 62 | } | ||
| 63 | inline double stop() { | ||
| 64 | double out = std::chrono::duration<double, std::ratio<1, 1>>( | ||
| 65 | std::chrono::high_resolution_clock::now() - p_) | ||
| 66 | .count(); | ||
| 67 | assert(has_been_started_ == true && "Stopwatch must be started first!"); | ||
| 68 | if (has_lapped_) { | ||
| 69 | has_lapped_ = false; | ||
| 70 | has_been_started_ = false; | ||
| 71 | return std::accumulate(durations_.begin(), durations_.end(), 0.0); | ||
| 72 | } | ||
| 73 | return out; | ||
| 74 | } | ||
| 75 | inline std::vector<double> get_data() { return durations_; } | ||
| 76 | }; | ||
| 77 | } // namespace IO | ||
| 78 | } // namespace Bembel | ||
| 79 | #endif // BEMBEL_SRC_IO_STOPWATCH_HPP_ | ||
| 80 |