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
45
46 //=============================================================================
47 //
48 // CLASS StopWatch
49 //
50 //=============================================================================
51
52
53 #ifndef ACG_STOPWATCH_HH
54 #define ACG_STOPWATCH_HH
55
56
57 //== INCLUDES =================================================================
58
59
60 #ifdef _WIN32
61
62 #include <windows.h>
63
64 #else // Linux
65
66 #include <sys/time.h>
67
68 #endif
69
70 #include "../Config/ACGDefines.hh"
71
72
73 //== NAMESPACES ===============================================================
74
75
76 namespace ACG {
77
78
79 //== CLASS DEFINITION =========================================================
80
81
82
90 class StopWatch
91 {
92 public:
93
95 StopWatch() {
96 #ifdef _WIN32
97 QueryPerformanceFrequency(&freq_);
98 #else
99 starttime_.tv_sec = 0;
100 starttime_.tv_usec = 0;
101 endtime_.tv_sec = 0;
102 endtime_.tv_usec = 0;
103 #endif
104 }
105
107 ~StopWatch() {}
108
110 void start() {
111 #ifdef _WIN32
112 QueryPerformanceCounter(&starttime_);
113 #else
114 starttime_ = current_time();
115 #endif
116 }
117
119 double restart() {
120 #ifdef _WIN32
121 QueryPerformanceCounter(&endtime_);
122 #else
123 endtime_ = current_time();
124 #endif
125
126 double t = elapsed();
127 start();
128 return t;
129 }
130
132 double stop() {
133 #ifdef _WIN32
134 QueryPerformanceCounter(&endtime_);
135 #else
136 endtime_ = current_time();
137 #endif
138
139 return elapsed();
140 }
141
143 double elapsed() const {
144 #ifdef _WIN32
145 return (double)(endtime_.QuadPart - starttime_.QuadPart)
146 / (double)freq_.QuadPart * 1000.0f;
147 #else
148 return ((endtime_.tv_sec - starttime_.tv_sec )*1000.0 +
149 (endtime_.tv_usec - starttime_.tv_usec)*0.001);
150 #endif
151 }
152
153
154 private:
155
156 #ifdef _WIN32
157 LARGE_INTEGER starttime_, endtime_;
158 LARGE_INTEGER freq_;
159 #else // Linux
160 timeval current_time() const {
161 struct timeval tv;
162 gettimeofday(&tv, 0);
163 return tv;
164 }
165
166 timeval starttime_, endtime_;
167 #endif
168
169 };
170
171
172 //=============================================================================
173 } // namespace ACG
174 //=============================================================================
175 #endif // ACG_STOPWATCH_HH defined
176 //=============================================================================
177
ACG
Namespace providing different geometric functions concerning angles.
Definition: BaseObjectData.hh:68
ACG::StopWatch::stop
double stop()
Stop time measurement, return time.
Definition: StopWatch.hh:132
ACG::StopWatch::start
void start()
Start time measurement.
Definition: StopWatch.hh:110
ACG::StopWatch
Definition: StopWatch.hh:90
ACG::StopWatch::~StopWatch
~StopWatch()
Destructor.
Definition: StopWatch.hh:107
ACG::StopWatch::StopWatch
StopWatch()
Constructor.
Definition: StopWatch.hh:95
ACG::StopWatch::elapsed
double elapsed() const
Get the total time in milli-seconds (watch has to be stopped).
Definition: StopWatch.hh:143
ACG::StopWatch::restart
double restart()
Restart, return time elapsed until now.
Definition: StopWatch.hh:119