MURE
Loading...
Searching...
No Matches
Timer.hxx
Go to the documentation of this file.
1/*
2 This file is part of MURE,
3 Copyright (C) 2007-2021 MURE developers.
4
5 MURE is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 MURE is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with MURE. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#include <chrono>
20// this has been borrowed from https://gist.github.com/mcleary/b0bf4fa88830ff7c882d
21// and was slightly adapted and extended for methods tick(), tock() and TickTock()
22
23class Timer
24{
25 public:
26 void start()
27 {
28 m_StartTime = std::chrono::high_resolution_clock::now();
29 m_bRunning = true;
30 }
31
32 void stop()
33 {
34 m_EndTime = std::chrono::high_resolution_clock::now();
35 m_bRunning = false;
36 }
37
38 void tick()
39 {
40 m_Tick = std::chrono::high_resolution_clock::now();
41 TickBeforeTock = (!TockAfterTick && true);
42 }
43
44 void tock()
45 {
46 m_Tock = std::chrono::high_resolution_clock::now();
47 TockAfterTick = (TickBeforeTock && true);
48 }
49
50 double TickTock()
51 {
52 double TT = -1;
54 {
55 TT = std::chrono::duration_cast<std::chrono::microseconds>(m_Tock - m_Tick).count();
56 }
57 TickBeforeTock = false;
58 TockAfterTick = false;
59 return TT;
60 }
61
63 {
64 std::chrono::time_point<std::chrono::high_resolution_clock> endTime;
65
66 if(m_bRunning)
67 {
68 endTime = std::chrono::high_resolution_clock::now();
69 }
70 else
71 {
72 endTime = m_EndTime;
73 }
74
75 return std::chrono::duration_cast<std::chrono::microseconds>(endTime - m_StartTime).count();
76 }
77
79 {
80 return elapsedMicroSeconds() / 1000000.0;
81 }
82
83 private:
84 // std::chrono::high_resolution_clock::time_point
85 std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTime;
86 std::chrono::time_point<std::chrono::high_resolution_clock> m_EndTime;
87 std::chrono::time_point<std::chrono::high_resolution_clock> m_Tick;
88 std::chrono::time_point<std::chrono::high_resolution_clock> m_Tock;
89
90 bool m_bRunning = false;
91 bool TickBeforeTock = false;
92 bool TockAfterTick = false;
93};
Definition Timer.hxx:24
std::chrono::time_point< std::chrono::high_resolution_clock > m_Tock
Definition Timer.hxx:88
bool TickBeforeTock
Definition Timer.hxx:91
void start()
start measurement
Definition Timer.hxx:26
double TickTock()
returns temporary time between tick() and tock()
Definition Timer.hxx:50
void stop()
Stop measurement.
Definition Timer.hxx:32
double elapsedSeconds()
return elapsed time in s since start (also before stop() was called)
Definition Timer.hxx:78
void tick()
temporary start
Definition Timer.hxx:38
std::chrono::time_point< std::chrono::high_resolution_clock > m_Tick
Definition Timer.hxx:87
double elapsedMicroSeconds()
return elapsed time in mus since start (also before stop() was called)
Definition Timer.hxx:62
bool m_bRunning
Definition Timer.hxx:90
void tock()
temporary stop
Definition Timer.hxx:44
std::chrono::time_point< std::chrono::high_resolution_clock > m_EndTime
Definition Timer.hxx:86
std::chrono::time_point< std::chrono::high_resolution_clock > m_StartTime
Definition Timer.hxx:85
bool TockAfterTick
Definition Timer.hxx:92

MURE Project, documentation generated by Doxygen 1.9.7 - Fri Jan 19 2024