Monero
Loading...
Searching...
No Matches
stopwatch.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2018-2019, tevador <tevador@gmail.com>
3
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, 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
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#pragma once
30
31#include <chrono>
32#include <cstdint>
33
34class Stopwatch {
35public:
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 }
68private:
69 using chrono_t = std::chrono::high_resolution_clock::time_point;
70 using sw_unit = std::chrono::nanoseconds;
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};
uint64_t elapsed
Definition stopwatch.hpp:72
void reset()
Definition stopwatch.hpp:42
double getElapsed() const
Definition stopwatch.hpp:65
void start()
Definition stopwatch.hpp:46
void stop()
Definition stopwatch.hpp:57
chrono_t startMark
Definition stopwatch.hpp:71
void restart()
Definition stopwatch.hpp:52
uint64_t getElapsedNanosec() const
Definition stopwatch.hpp:75
std::chrono::nanoseconds sw_unit
Definition stopwatch.hpp:70
bool isRunning
Definition stopwatch.hpp:73
Stopwatch(bool startNow=false)
Definition stopwatch.hpp:36
std::chrono::high_resolution_clock::time_point chrono_t
Definition stopwatch.hpp:69
unsigned __int64 uint64_t
Definition stdint.h:136