Minotaur 0.4.1
Docs for developers
Timer.h
Go to the documentation of this file.
1//
2// Minotaur -- It's only 1/2 bull
3//
4// (C)opyright 2009 - 2025 The Minotaur Team.
5//
6
14#ifndef MINOTAURTIMER_H
15#define MINOTAURTIMER_H
16
17#include <time.h>
18
19#ifndef CLOCKS_PER_SEC
20#define CLOCKS_PER_SEC 1000000
21#endif
22
23#ifdef MINOTAUR_RUSAGE
24#include <sys/resource.h>
25
26#ifndef MICROSEC
27#define MICROSEC 1000000
28#endif
29#endif
30
31#include "Types.h"
32#include <chrono>
33namespace Minotaur {
40 class Timer {
41 public:
42 Timer() {};
43 virtual ~Timer() {};
44 virtual void start() = 0;
45 virtual void stop() = 0;
46 virtual double query() const = 0;
47 // New function to query wall time
48 virtual double wQuery() const
49 {
50 using namespace std::chrono;
51 steady_clock::time_point currentTime = steady_clock::now();
52 duration<double> time_span =
53 duration_cast<duration<double>>(currentTime - w_);
54 return time_span.count();
55 }
56
57 protected:
58 std::chrono::steady_clock::time_point w_;
59 bool is_started_ = false;
60
61 private:
62 Timer(const Timer &);
63 Timer &operator=(const Timer &);
64 };
65
70 class ClockTimer : public Timer {
71 private:
72 clock_t s_;
73 bool is_started_;
74
75 public:
77 : is_started_(false) {};
78 ~ClockTimer() {};
79
81 void start()
82 {
83 w_ = std::chrono::steady_clock::now();
84 s_ = clock();
85 is_started_ = true;
86 return;
87 };
88
90 void stop()
91 {
92 is_started_ = false;
93 return;
94 };
95
96 double query() const
97 {
98 if (!is_started_) {
99 throw("Some exception");
100 }
101
102 return ((double)(clock() - s_) / (double)CLOCKS_PER_SEC);
103 };
104 };
105
106#ifdef MINOTAUR_RUSAGE
111 class UsageTimer : public Timer {
112 private:
113 struct rusage s_;
114 bool is_started_;
115
116 public:
117 UsageTimer()
118 : is_started_(false) {};
119 ~UsageTimer() {};
120
122 void start()
123 {
124 getrusage(RUSAGE_SELF, &s_);
125 is_started_ = true;
126 return;
127 };
128
130 void stop()
131 {
132 is_started_ = false;
133 return;
134 };
135
137 double query() const
138 {
139 double t1, t2, m1, m2, s1, s2, m, s;
140 struct rusage e;
141 if (!is_started_) {
142 throw("Some exception");
143 }
144 getrusage(RUSAGE_SELF, &e);
145
146 m1 = (double)e.ru_utime.tv_usec;
147 m2 = (double)s_.ru_utime.tv_usec;
148 m = m1 - m2;
149
150 s1 = (double)e.ru_utime.tv_sec;
151 s2 = (double)s_.ru_utime.tv_sec;
152 s = s1 - s2;
153
154 t1 = s + m / MICROSEC;
155
156 m1 = (double)e.ru_stime.tv_usec;
157 m2 = (double)s_.ru_stime.tv_usec;
158 m = m1 - m2;
159
160 s1 = (double)e.ru_stime.tv_sec;
161 s2 = (double)s_.ru_stime.tv_sec;
162 s = s1 - s2;
163
164 t2 = s + m / MICROSEC;
165
166 return t1 + t2;
167 };
168 };
169#endif
170
173 public:
176
178 virtual ~TimerFactory() {};
179
181 virtual Timer *getTimer()
182 {
183#ifdef MINOTAUR_RUSAGE
184 return new UsageTimer;
185#else
186 return new ClockTimer;
187#endif
188 };
189
190 private:
191 TimerFactory(const TimerFactory &);
192 TimerFactory &operator=(const TimerFactory &);
193 };
194} //namespace Minotaur
195
196#endif
Declare important 'types' used in Minotaur.
Definition: Timer.h:70
void stop()
Stop the timer. Can not query after this.
Definition: Timer.h:90
void start()
Start the timer.
Definition: Timer.h:81
The TimerFactory should be used to get the approrpriate Timer.
Definition: Timer.h:172
virtual ~TimerFactory()
Destroy.
Definition: Timer.h:178
TimerFactory()
Default constructor.
Definition: Timer.h:175
virtual Timer * getTimer()
Return an appropriate Timer.
Definition: Timer.h:181
Definition: Timer.h:40
Definition: ActiveNodeStore.h:20

Minotaur source code documented by Doxygen 1.9.4 on Thu Apr 24 2025