SheafSystem  0.0.0.0
stop_watch.cc
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
18 // Implementation for class stop_watch
19 
20 #include "SheafSystem/stop_watch.h"
21 
22 #include "SheafSystem/assert_contract.h"
23 #include "SheafSystem/std_iostream.h"
24 
25 // ===========================================================
26 // ANY FACET
27 // ===========================================================
28 
29 
30 
31 bool
33 invariant() const
34 {
35  bool result = true;
36 
37  invariance(_start_time <= _stop_time);
38 
39  // Exit
40 
41  return result;
42 }
43 
44 // ===========================================================
45 // STOP_WATCH FACET
46 // ===========================================================
47 
50 {
51 
52  // Preconditions:
53 
54  // Body:
55 
56  reset();
57 
58  // Postconditions:
59 
60  ensure(invariant());
61 
62  // Exit:
63 
64  return;
65 }
66 
69 {
70  // Preconditions:
71 
72  // Body:
73 
74  // Nothing to do.
75 
76  // Postconditions:
77 
78  // Exit:
79 
80  return;
81 }
82 
83 void
86 {
87  // Preconditions:
88 
89  // Body:
90 
91  _start_time = clock();
92 
93  // Postconditions:
94 
95  // Exit:
96 
97  return;
98 }
99 
100 void
103 {
104  // Preconditions:
105 
106  // Body:
107 
108  _stop_time = clock();
109 
110  _cumulative_time += time();
111 
112  // Postconditions:
113 
114  // Exit:
115 
116  return;
117 }
118 
119 double
121 time(time_unit xunit) const
122 {
123  double result;
124 
125  // Preconditions:
126 
127  // Body:
128 
129  double ldiff = (double) (_stop_time - _start_time);
130 
131  switch(xunit)
132  {
133  case CPU_TIME:
134  result = ldiff;
135  break;
136 
137  case MILLISECONDS:
138  result = (ldiff / CLOCKS_PER_SEC) * 1000.0;
139  break;
140 
141  default:
142  result = ldiff / CLOCKS_PER_SEC;
143  break;
144  }
145 
146  // Postconditions:
147 
148 
149  // Exit:
150 
151  return result;
152 }
153 
155 double
158 {
159  // Preconditions:
160 
161 
162  // Body:
163 
164  // Postconditions:
165 
166 
167  // Exit:
168 
169  return _cumulative_time;
170 }
171 
172 
174 void
177 {
178  // Preconditions:
179 
180  // Body:
181 
182  stop();
183  _lap_times.push_back(time());
184  _start_time = _stop_time;
185 
186  // Postconditions:
187 
188 
189  // Exit:
190 
191  return;
192 }
193 
195 double
197 lap_time(int xi) const
198 {
199  double result;
200 
201  // Preconditions:
202 
203  require((0 <= xi) && (xi <= lap_ct()));
204 
205  // Body:
206 
207  result = _lap_times[xi];
208 
209  // Postconditions:
210 
211 
212  // Exit:
213 
214  return result;
215 }
216 
217 
219 size_t
221 lap_ct() const
222 {
223  size_t result;
224 
225  // Preconditions:
226 
227 
228  // Body:
229 
230  result = _lap_times.size();
231 
232  // Postconditions:
233 
234  // Exit:
235 
236  return result;
237 }
238 
240 void
243 {
244  // Preconditions:
245 
246 
247  // Body:
248 
249  _start_time = 0;
250  _stop_time = 0;
251  _cumulative_time = 0;
252  _lap_times.clear();
253 
254  // Postconditions:
255 
256  ensure(time() == 0);
257  ensure(lap_ct() == 0);
258 
259  // Exit:
260 
261  return;
262 }
263 
264 // ===========================================================
265 // NON-MEMBER FUNCTIONS
266 // ===========================================================
267 
268 std::ostream&
269 sheaf::operator << (std::ostream &xos, stop_watch& xsw)
270 {
271  xos << " lap times: ";
272 
273  for(size_t i=0; i<xsw.lap_ct(); ++i)
274  {
275  xos << " " << xsw.lap_time(i);
276  if((i % 10 == 9) || (i == (xsw.lap_ct() - 1)))
277  {
278  xos << std::endl;
279  }
280  }
281 
282  return xos;
283 }
void stop()
Marks the end of an interval.
Definition: stop_watch.cc:102
double cumulative_time() const
The accumulated time in seconds between start()&#39;s and stop()&#39;s since the last reset().
Definition: stop_watch.cc:157
double lap_time(int i) const
The length in seconds of the xi-th lap.
Definition: stop_watch.cc:197
size_t lap_ct() const
The number of laps that have be marked.
Definition: stop_watch.cc:221
void reset()
Clears the lap times and sets lap_ct() to 0.
Definition: stop_watch.cc:242
stop_watch()
Default constructor.
Definition: stop_watch.cc:49
time_unit
The unit of time to return.
Definition: stop_watch.h:82
void mark_lap()
Marks the end of the current lap.
Definition: stop_watch.cc:176
virtual ~stop_watch()
Destructor.
Definition: stop_watch.cc:68
SHEAF_DLL_SPEC std::ostream & operator<<(std::ostream &os, const dof_descriptor_array &p)
Insert dof_descriptor_array& p into ostream& os.
void start()
Marks the start of an interval.
Definition: stop_watch.cc:85
double time(time_unit xunit=SECONDS) const
The length of the current interval.
Definition: stop_watch.cc:121
virtual bool invariant() const
Class invariant.
Definition: stop_watch.cc:33
A clock for timing intervals.
Definition: stop_watch.h:45