Monero
stopwatch.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2018-2019, tevador <tevador@gmail.com>
3 
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the copyright holder nor the
14  names of its contributors may be used to endorse or promote products
15  derived from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #pragma once
30 
31 #include <chrono>
32 #include <cstdint>
33 
34 class Stopwatch {
35 public:
36  Stopwatch(bool startNow = false) {
37  reset();
38  if (startNow) {
39  start();
40  }
41  }
42  void reset() {
43  isRunning = false;
44  elapsed = 0;
45  }
46  void start() {
47  if (!isRunning) {
48  startMark = std::chrono::high_resolution_clock::now();
49  isRunning = true;
50  }
51  }
52  void restart() {
53  startMark = std::chrono::high_resolution_clock::now();
54  isRunning = true;
55  elapsed = 0;
56  }
57  void stop() {
58  if (isRunning) {
59  chrono_t endMark = std::chrono::high_resolution_clock::now();
60  uint64_t ns = std::chrono::duration_cast<sw_unit>(endMark - startMark).count();
61  elapsed += ns;
62  isRunning = false;
63  }
64  }
65  double getElapsed() const {
66  return getElapsedNanosec() / 1e+9;
67  }
68 private:
69  using chrono_t = std::chrono::high_resolution_clock::time_point;
70  using sw_unit = std::chrono::nanoseconds;
73  bool isRunning;
74 
76  uint64_t elns = elapsed;
77  if (isRunning) {
78  chrono_t endMark = std::chrono::high_resolution_clock::now();
79  uint64_t ns = std::chrono::duration_cast<sw_unit>(endMark - startMark).count();
80  elns += ns;
81  }
82  return elns;
83  }
84 };
std::chrono::nanoseconds sw_unit
Definition: stopwatch.hpp:70
int * count
Definition: gmock_stress_test.cc:176
std::chrono::high_resolution_clock::time_point chrono_t
Definition: stopwatch.hpp:69
Definition: stopwatch.hpp:34
void restart()
Definition: stopwatch.hpp:52
void stop()
Definition: stopwatch.hpp:57
uint64_t elapsed
Definition: stopwatch.hpp:72
e
Definition: pymoduletest.py:79
chrono_t startMark
Definition: stopwatch.hpp:71
unsigned __int64 uint64_t
Definition: stdint.h:136
Stopwatch(bool startNow=false)
Definition: stopwatch.hpp:36
double getElapsed() const
Definition: stopwatch.hpp:65
void start()
Definition: stopwatch.hpp:46
bool isRunning
Definition: stopwatch.hpp:73
void reset()
Definition: stopwatch.hpp:42
uint64_t getElapsedNanosec() const
Definition: stopwatch.hpp:75