Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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 
39 namespace ogdf {
40 
42 
46  int64_t m_startTime;
47  int64_t m_totalTime;
48  bool m_running;
49 
50 public:
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 
145 protected:
147 
153  virtual int64_t theTime() const = 0;
154 };
155 
158 public:
160 
165 
167 
173  explicit StopwatchCPU(int64_t milliSecs) : Stopwatch(milliSecs) { }
174 
175  virtual ~StopwatchCPU() { }
176 
177 private:
179  virtual int64_t theTime() const override;
180 };
181 
184 public:
186 
191 
193 
199  explicit StopwatchWallClock(int64_t milliSecs) : Stopwatch(milliSecs) { }
200 
201  virtual ~StopwatchWallClock() { }
202 
203 private:
205  virtual int64_t theTime() const override;
206 };
207 
208 }
ogdf::Stopwatch::milliSeconds
int64_t milliSeconds() const
Returns the currently elapsed time in milliseconds.
Definition: Stopwatch.h:97
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::StopwatchWallClock
Implements a stopwatch measuring wall-clock time.
Definition: Stopwatch.h:183
ogdf::StopwatchWallClock::StopwatchWallClock
StopwatchWallClock()
Creates a stopwatch for measuring wall-clock time with total time 0.
Definition: Stopwatch.h:190
backward::Color::reset
@ reset
Definition: backward.hpp:1719
ogdf::Stopwatch::m_startTime
int64_t m_startTime
The start time of the timer in milliseconds.
Definition: Stopwatch.h:46
ogdf::Stopwatch::~Stopwatch
virtual ~Stopwatch()
Definition: Stopwatch.h:68
ogdf::StopwatchCPU
Implements a stopwatch measuring CPU time.
Definition: Stopwatch.h:157
ogdf::Stopwatch::hours
int64_t hours() const
Returns the currently elapsed time in hours.
Definition: Stopwatch.h:126
ogdf::StopwatchCPU::StopwatchCPU
StopwatchCPU()
Creates a stopwatch for measuring CPU time with total time 0.
Definition: Stopwatch.h:164
ogdf::Stopwatch::addCentiSeconds
void addCentiSeconds(int64_t centiSeconds)
Adds centiSeconds to total time.
Definition: Stopwatch.h:135
ogdf::Stopwatch::Stopwatch
Stopwatch(int64_t milliSecs)
Initializes a stopwatch and sets its total time to milliSecs.
Definition: Stopwatch.h:65
ogdf::Stopwatch::seconds
int64_t seconds() const
Returns the currently elapsed time in seconds.
Definition: Stopwatch.h:112
ogdf::Stopwatch::Stopwatch
Stopwatch()
Initializes a stop watch with total time 0.
Definition: Stopwatch.h:56
ogdf::Stopwatch::m_totalTime
int64_t m_totalTime
The total time in milliseconds.
Definition: Stopwatch.h:47
ogdf::StopwatchWallClock::StopwatchWallClock
StopwatchWallClock(int64_t milliSecs)
Creates a stopwatch for measuring wall-clock time and sets its total time to milliSecs.
Definition: Stopwatch.h:199
ogdf::operator<<
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:983
ogdf::Stopwatch::minutes
int64_t minutes() const
Returns the currently elapsed time in minutes.
Definition: Stopwatch.h:119
ogdf::Stopwatch::centiSeconds
int64_t centiSeconds() const
Returns the currently elapsed time in 1/100-seconds.
Definition: Stopwatch.h:105
ogdf::Stopwatch::running
bool running() const
Returns true if the stopwatch is running, false otherwise.
Definition: Stopwatch.h:91
ogdf::Stopwatch::m_running
bool m_running
true, if the timer is running.
Definition: Stopwatch.h:48
ogdf::StopwatchCPU::~StopwatchCPU
virtual ~StopwatchCPU()
Definition: Stopwatch.h:175
basic.h
Basic declarations, included by all source files.
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
ogdf::Stopwatch
Realizes a stopwatch for measuring elapsed time.
Definition: Stopwatch.h:45
ogdf::StopwatchCPU::StopwatchCPU
StopwatchCPU(int64_t milliSecs)
Creates a stopwatch for measuring CPU time and sets its total time to milliSecs.
Definition: Stopwatch.h:173
ogdf::StopwatchWallClock::~StopwatchWallClock
virtual ~StopwatchWallClock()
Definition: Stopwatch.h:201
ogdf::Stopwatch::reset
void reset()
Stops the stopwatch and sets its total time to 0.
Definition: Stopwatch.h:85
ogdf::Stopwatch::exceeds
bool exceeds(int64_t maxSeconds) const
Returns true iff the currently elapsed time exceeds maxSeconds.
Definition: Stopwatch.h:129