Developer Documentation
StopWatch.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS StopWatch
55 //
56 //=============================================================================
57 
58 
59 #ifndef ACG_STOPWATCH_HH
60 #define ACG_STOPWATCH_HH
61 
62 
63 //== INCLUDES =================================================================
64 
65 
66 #ifdef _WIN32
67 
68 #include <windows.h>
69 
70 #else // Linux
71 
72 #include <sys/time.h>
73 
74 #endif
75 
76 #include "../Config/ACGDefines.hh"
77 
78 
79 //== NAMESPACES ===============================================================
80 
81 
82 namespace ACG {
83 
84 
85 //== CLASS DEFINITION =========================================================
86 
87 
88 
96 class StopWatch
97 {
98 public:
99 
102  #ifdef _WIN32
103  QueryPerformanceFrequency(&freq_);
104  #else
105  starttime_.tv_sec = 0;
106  starttime_.tv_usec = 0;
107  endtime_.tv_sec = 0;
108  endtime_.tv_usec = 0;
109  #endif
110  }
111 
114 
116  void start() {
117  #ifdef _WIN32
118  QueryPerformanceCounter(&starttime_);
119  #else
120  starttime_ = current_time();
121  #endif
122  }
123 
125  double restart() {
126  #ifdef _WIN32
127  QueryPerformanceCounter(&endtime_);
128  #else
129  endtime_ = current_time();
130  #endif
131 
132  double t = elapsed();
133  start();
134  return t;
135  }
136 
138  double stop() {
139  #ifdef _WIN32
140  QueryPerformanceCounter(&endtime_);
141  #else
142  endtime_ = current_time();
143  #endif
144 
145  return elapsed();
146  }
147 
149  double elapsed() const {
150  #ifdef _WIN32
151  return (double)(endtime_.QuadPart - starttime_.QuadPart)
152  / (double)freq_.QuadPart * 1000.0f;
153  #else
154  return ((endtime_.tv_sec - starttime_.tv_sec )*1000.0 +
155  (endtime_.tv_usec - starttime_.tv_usec)*0.001);
156  #endif
157  }
158 
159 
160 private:
161 
162  #ifdef _WIN32
163  LARGE_INTEGER starttime_, endtime_;
164  LARGE_INTEGER freq_;
165  #else // Linux
166  timeval current_time() const {
167  struct timeval tv;
168  gettimeofday(&tv, 0);
169  return tv;
170  }
171 
172  timeval starttime_, endtime_;
173  #endif
174 
175 };
176 
177 
178 //=============================================================================
179 } // namespace ACG
180 //=============================================================================
181 #endif // ACG_STOPWATCH_HH defined
182 //=============================================================================
183 
double restart()
Restart, return time elapsed until now.
Definition: StopWatch.hh:125
StopWatch()
Constructor.
Definition: StopWatch.hh:101
~StopWatch()
Destructor.
Definition: StopWatch.hh:113
double elapsed() const
Get the total time in milli-seconds (watch has to be stopped).
Definition: StopWatch.hh:149
void start()
Start time measurement.
Definition: StopWatch.hh:116
double stop()
Stop time measurement, return time.
Definition: StopWatch.hh:138
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51