-
Notifications
You must be signed in to change notification settings - Fork 39
/
pt.h
184 lines (157 loc) · 4.51 KB
/
pt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
Performance timers for Windows and OSX
*/
#ifndef PT_H
#define PT_H
#ifndef PLATFORM_SELECTOR_H
#include "platform_selector.h"
#endif
namespace Chordia
{
//-----------------------------------------------------------------------------
// Snap a performance counter tick value
//-----------------------------------------------------------------------------
#ifdef _WINDOWS_
typedef int64_t Interval;
#else
typedef uint64_t Interval;
#endif
class CPTSnap
{
Interval m_value;
public:
CPTSnap()
{
#if defined(_WINDOWS_)
QueryPerformanceCounter((LARGE_INTEGER*)&m_value);
#elif defined(__MACH__)
// see http://developer.apple.com/library/mac/#qa/qa1398/_index.html
m_value = mach_absolute_time();
#else
m_value = 0;
#endif
}
CPTSnap(const CPTSnap& arg)
{
m_value = arg.value();
}
explicit CPTSnap(const Interval& arg)
{
m_value = arg;
}
CPTSnap& operator=(const CPTSnap& arg)
{
m_value = arg.value();
return (*this);
}
//
Interval value() const { return m_value; }
};
//-----------------------------------------------------------------------------
// Defines a time interval between 2 CPTSnap instantiations
//-----------------------------------------------------------------------------
class CPTInterval
{
// the interval value. arbitrary ticks, no time value yet
Interval m_interval;
// calculate difference in snaps
Interval Diff(const CPTSnap& pts, const CPTSnap& pte)
{
Interval ret = 0;
ret = pte.value() - pts.value();
return ret;
}
//
enum Unit
{
eSeconds,
eMilliseconds,
eMicroSeconds,
eNanoSeconds,
};
// convert to time value in seconds
double Scale(Unit unit) const
{
#if defined(_WINDOWS_)
LARGE_INTEGER li;
// what is the frequency ?
QueryPerformanceFrequency(&li);
double frequency = static_cast<double>(li.QuadPart);
double seconds = 0;
// remember. ticker frequency on Windows is per second ...
switch (unit)
{
case eSeconds:
seconds = (m_interval * 1) / frequency;
break;
case eMilliseconds:
seconds = (m_interval * 1000) / frequency;
break;
case eMicroSeconds:
seconds = (m_interval * 1000000) / frequency;
break;
case eNanoSeconds:
seconds = (m_interval * 1000000000) / frequency;
break;
}
return seconds;
#elif defined(__MACH__)
// MacOS gives us intervals in nanoseconds ...
mach_timebase_info_data_t info;
mach_timebase_info(&info);
uint64_t nanos = (m_interval * info.numer) / info.denom;
switch (unit)
{
case eSeconds:
nanos /= 1000000000;
break;
case eMilliseconds:
nanos /= 1000000;
break;
case eMicroSeconds:
nanos /= 1000;
break;
case eNanoSeconds:
break;
}
// hmm ...
return static_cast<double>(nanos);
#else
return 0;
#endif
}
public:
//
CPTInterval()
: m_interval(0)
{
}
//
CPTInterval(const CPTSnap& pts, const CPTSnap& pte)
: m_interval(0)
{
// calculate the interval
m_interval = Diff(pts,pte);
}
// short cut
CPTInterval(const CPTSnap& pts)
: m_interval(0)
{
// calculate the interval
m_interval = Diff(pts,CPTSnap());
}
// get the interval in ms
double ms() const
{
// convert to a time period
return Scale(eMilliseconds);
}
// get the interval in us
double us() const
{
// convert to a time period
return Scale(eMicroSeconds);
}
}; // class
} // namespace
#endif