Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
Stopwatch.h
Go to the documentation of this file.
1
32#pragma once
33
34#include <ogdf/basic/basic.h>
35
36#include <cstdint>
37#include <iosfwd>
38
39namespace ogdf {
40
42
46 int64_t m_startTime;
47 int64_t m_totalTime;
48 bool m_running;
49
50public:
52
56 Stopwatch() : m_startTime(0), m_totalTime(0), m_running(false) { }
57
59
65 explicit Stopwatch(int64_t milliSecs)
66 : m_startTime(0), m_totalTime(milliSecs), m_running(false) { }
67
68 virtual ~Stopwatch() { }
69
71
76 void start(bool reset = false);
77
79
82 void stop();
83
85 void reset() {
86 m_running = false;
87 m_totalTime = 0;
88 }
89
91 bool running() const { return m_running; }
92
94
97 int64_t milliSeconds() const {
98 return m_running ? m_totalTime + theTime() - m_startTime : m_totalTime;
99 }
100
102
105 int64_t centiSeconds() const { return milliSeconds() / 10; }
106
108
112 int64_t seconds() const { return milliSeconds() / 1000; }
113
115
119 int64_t minutes() const { return seconds() / 60; }
120
122
126 int64_t hours() const { return seconds() / 3600; }
127
129 bool exceeds(int64_t maxSeconds) const { return seconds() >= maxSeconds; }
130
132
135 void addCentiSeconds(int64_t centiSeconds) { m_totalTime += 10 * centiSeconds; }
136
138
143 friend OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const Stopwatch& stopwatch);
144
145protected:
147
153 virtual int64_t theTime() const = 0;
154};
155
158public:
160
165
167
173 explicit StopwatchCPU(int64_t milliSecs) : Stopwatch(milliSecs) { }
174
175 virtual ~StopwatchCPU() { }
176
177private:
179 virtual int64_t theTime() const override;
180};
181
184public:
186
191
193
199 explicit StopwatchWallClock(int64_t milliSecs) : Stopwatch(milliSecs) { }
200
202
203private:
205 virtual int64_t theTime() const override;
206};
207
208}
Basic declarations, included by all source files.
Implements a stopwatch measuring CPU time.
Definition Stopwatch.h:157
virtual ~StopwatchCPU()
Definition Stopwatch.h:175
StopwatchCPU(int64_t milliSecs)
Creates a stopwatch for measuring CPU time and sets its total time to milliSecs.
Definition Stopwatch.h:173
StopwatchCPU()
Creates a stopwatch for measuring CPU time with total time 0.
Definition Stopwatch.h:164
virtual int64_t theTime() const override
Returns the current CPU time in milliseconds (from some fixed starting point).
Realizes a stopwatch for measuring elapsed time.
Definition Stopwatch.h:45
int64_t m_totalTime
The total time in milliseconds.
Definition Stopwatch.h:47
friend std::ostream & operator<<(std::ostream &os, const Stopwatch &stopwatch)
Writes the currently elapsed time in the format hh:mm:ss.sec/100 to output stream os.
int64_t hours() const
Returns the currently elapsed time in hours.
Definition Stopwatch.h:126
int64_t centiSeconds() const
Returns the currently elapsed time in 1/100-seconds.
Definition Stopwatch.h:105
Stopwatch()
Initializes a stop watch with total time 0.
Definition Stopwatch.h:56
bool m_running
true, if the timer is running.
Definition Stopwatch.h:48
int64_t m_startTime
The start time of the timer in milliseconds.
Definition Stopwatch.h:46
void start(bool reset=false)
Starts the stopwatch.
Stopwatch(int64_t milliSecs)
Initializes a stopwatch and sets its total time to milliSecs.
Definition Stopwatch.h:65
void addCentiSeconds(int64_t centiSeconds)
Adds centiSeconds to total time.
Definition Stopwatch.h:135
void reset()
Stops the stopwatch and sets its total time to 0.
Definition Stopwatch.h:85
int64_t seconds() const
Returns the currently elapsed time in seconds.
Definition Stopwatch.h:112
void stop()
Stops the stopwatch and adds the difference between the current time and the starting time to the tot...
int64_t minutes() const
Returns the currently elapsed time in minutes.
Definition Stopwatch.h:119
bool exceeds(int64_t maxSeconds) const
Returns true iff the currently elapsed time exceeds maxSeconds.
Definition Stopwatch.h:129
bool running() const
Returns true if the stopwatch is running, false otherwise.
Definition Stopwatch.h:91
int64_t milliSeconds() const
Returns the currently elapsed time in milliseconds.
Definition Stopwatch.h:97
virtual ~Stopwatch()
Definition Stopwatch.h:68
virtual int64_t theTime() const =0
Returns the current time in milliseconds (from some fixed starting point).
Implements a stopwatch measuring wall-clock time.
Definition Stopwatch.h:183
StopwatchWallClock()
Creates a stopwatch for measuring wall-clock time with total time 0.
Definition Stopwatch.h:190
StopwatchWallClock(int64_t milliSecs)
Creates a stopwatch for measuring wall-clock time and sets its total time to milliSecs.
Definition Stopwatch.h:199
virtual int64_t theTime() const override
Returns the current wall-clock time in milliseconds (from some fixed starting point).
virtual ~StopwatchWallClock()
Definition Stopwatch.h:201
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
The namespace for all OGDF objects.