Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/include/performanceCounter.h

raw symbols SCIP
1 /*
2
3 * Copyright (c) 2008, Carnegie Mellon University
4 * All rights reserved.
5 *
6 * Code author: Jernej Barbic
7 * Research: Jernej Barbic, Doug L. James
8 * Funding: NSF, Link Foundation
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * * Neither the name of Carnegie Mellon University, nor the
18 * names of its contributors may be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32
33 === A counter to measure code execution time. ===
34
35 Designed for real-time system (e.g., real-time computer animation, haptics),
36 but useful in general. You can time arbitrary segments of your code.
37 Same interface under Windows, Linux and Mac OS X.
38
39 Under Linux/MAC OS X, accuracy is that of gettimeofday function,
40 which gives time in seconds and microseconds.
41 In practice, it has been accurate down to microsecond range.
42
43 Under Windows, the counter uses the QueryPerformanceCounter Windows API call.
44 Again, accuracy has been in the microsecond range in practice.
45
46 Usage:
47 Call StartCounter() before your code block.
48 Call StopCounter() after your code block.
49 Read the elapsed time using GetElapsedTime().
50
51 Version: 1.0
52
53 */
54
55 #ifndef _PERFORMANCECOUNTER_H_
56 #define _PERFORMANCECOUNTER_H_
57
58 /**************** LINUX/MAC OS X COUNTER *******************/
59
60 #if (defined __unix__) || (defined __APPLE__)
61
62 #include "stdlib.h"
63 #include "sys/time.h"
64
65 class PerformanceCounter
66 {
67 public:
68
69 PerformanceCounter() { StartCounter(); }
70
71 void StartCounter(); // call this before your code block
72 void StopCounter(); // call this after your code block
73
74 // read the elapsed time (units are seconds, accuracy is up to microseconds)
75 double GetElapsedTime();
76
77 protected:
78 long startCountSec,stopCountSec,startCountMicroSec,stopCountMicroSec;
79 };
80
81 inline void PerformanceCounter::StartCounter()
82 {
83 struct timeval tv;
84
85 gettimeofday(&tv,NULL);
86
87 startCountSec = tv.tv_sec;
88 startCountMicroSec = tv.tv_usec;
89 stopCountSec = startCountSec;
90 stopCountMicroSec = startCountMicroSec;
91 }
92
93 inline void PerformanceCounter::StopCounter()
94 {
95 struct timeval tv;
96
97 gettimeofday(&tv,NULL);
98
99 stopCountSec = tv.tv_sec;
100 stopCountMicroSec = tv.tv_usec;
101 }
102
103
104 inline double PerformanceCounter::GetElapsedTime()
105 {
106 double elapsedTime = 1.0 * (stopCountSec-startCountSec) + 1E-6 * (stopCountMicroSec - startCountMicroSec);
107 return elapsedTime;
108 }
109
110 #endif
111
112 #if defined(_WIN32) || defined(WIN32)
113
114 /**************** WINDOWS COUNTER *******************/
115
116 #include <windows.h>
117
118 class PerformanceCounter
119 {
120 public:
121
122 PerformanceCounter()
123 {
124 // reset the counter frequency
125 QueryPerformanceFrequency(&timerFrequency);
126 StartCounter();
127 }
128
129 void StartCounter(); // call this before your code block
130 void StopCounter(); // call this after your code block
131
132 // read elapsed time (units are seconds, accuracy is up to microseconds)
133 double GetElapsedTime();
134
135 protected:
136 LARGE_INTEGER timerFrequency;
137 LARGE_INTEGER startCount,stopCount;
138 };
139
140 inline void PerformanceCounter::StartCounter()
141 {
142 QueryPerformanceCounter(&startCount);
143 }
144
145 inline void PerformanceCounter::StopCounter()
146 {
147 QueryPerformanceCounter(&stopCount);
148 }
149
150
151 inline double PerformanceCounter::GetElapsedTime()
152 {
153 return ((double)(stopCount.QuadPart - startCount.QuadPart))
154 / ((double)timerFrequency.QuadPart);
155 }
156
157 #endif
158 #endif
159