blob: 6c5368d0aef937b31f7a761c87ca268e0c61295e (
plain)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
#ifndef __HAYAI_TESTRESULT
#define __HAYAI_TESTRESULT
#include <vector>
#include <stdexcept>
#include <limits>
#include <cmath>
#include "hayai_clock.hpp"
namespace hayai
{
/// Test result descriptor.
/// All durations are expressed in nanoseconds.
struct TestResult
{
public:
/// Initialize test result descriptor.
/// @param runTimes Timing for the individual runs.
/// @param iterations Number of iterations per run.
TestResult(const std::vector<uint64_t>& runTimes,
std::size_t iterations)
: _runTimes(runTimes),
_iterations(iterations),
_timeTotal(0),
_timeRunMin(std::numeric_limits<uint64_t>::max()),
_timeRunMax(std::numeric_limits<uint64_t>::min()),
_timeStdDev(0.0),
_timeMedian(0.0),
_timeQuartile1(0.0),
_timeQuartile3(0.0)
{
// Summarize under the assumption of values being accessed more
// than once.
std::vector<uint64_t>::iterator runIt = _runTimes.begin();
while (runIt != _runTimes.end())
{
const uint64_t run = *runIt;
_timeTotal += run;
if ((runIt == _runTimes.begin()) || (run > _timeRunMax))
_timeRunMax = run;
if ((runIt == _runTimes.begin()) || (run < _timeRunMin))
_timeRunMin = run;
++runIt;
}
// Calculate standard deviation.
const double mean = RunTimeAverage();
double accu = 0.0;
runIt = _runTimes.begin();
while (runIt != _runTimes.end())
{
const uint64_t run = *runIt;
const double diff = double(run) - mean;
accu += (diff * diff);
++runIt;
}
_timeStdDev = std::sqrt(accu / (_runTimes.size() - 1));
// Calculate quartiles.
std::vector<uint64_t> sortedRunTimes(_runTimes);
std::sort(sortedRunTimes.begin(), sortedRunTimes.end());
const std::size_t sortedSize = sortedRunTimes.size();
const std::size_t sortedSizeHalf = sortedSize / 2;
if (sortedSize >= 2)
{
const std::size_t quartile = sortedSizeHalf / 2;
if ((sortedSize % 2) == 0)
{
_timeMedian =
(double(sortedRunTimes[sortedSizeHalf - 1]) +
double(sortedRunTimes[sortedSizeHalf])) / 2;
_timeQuartile1 =
double(sortedRunTimes[quartile]);
_timeQuartile3 =
double(sortedRunTimes[sortedSizeHalf + quartile]);
}
else
{
_timeMedian = double(sortedRunTimes[sortedSizeHalf]);
_timeQuartile1 =
(double(sortedRunTimes[quartile - 1]) +
double(sortedRunTimes[quartile])) / 2;
_timeQuartile3 = (
double(
sortedRunTimes[sortedSizeHalf + (quartile - 1)]
) +
double(
sortedRunTimes[sortedSizeHalf + quartile]
)
) / 2;
}
}
else if (sortedSize > 0)
{
_timeQuartile1 = double(sortedRunTimes[0]);
_timeQuartile3 = _timeQuartile1;
}
}
/// Total time.
inline double TimeTotal() const
{
return double(_timeTotal);
}
/// Run times.
inline const std::vector<uint64_t>& RunTimes() const
{
return _runTimes;
}
/// Average time per run.
inline double RunTimeAverage() const
{
return double(_timeTotal) / double(_runTimes.size());
}
/// Standard deviation time per run.
inline double RunTimeStdDev() const
{
return _timeStdDev;
}
/// Median (2nd Quartile) time per run.
inline double RunTimeMedian() const
{
return _timeMedian;
}
/// 1st Quartile time per run.
inline double RunTimeQuartile1() const
{
return _timeQuartile1;
}
/// 3rd Quartile time per run.
inline double RunTimeQuartile3() const
{
return _timeQuartile3;
}
/// Maximum time per run.
inline double RunTimeMaximum() const
{
return double(_timeRunMax);
}
/// Minimum time per run.
inline double RunTimeMinimum() const
{
return double(_timeRunMin);
}
/// Average runs per second.
inline double RunsPerSecondAverage() const
{
return 1000000000.0 / RunTimeAverage();
}
/// Median (2nd Quartile) runs per second.
inline double RunsPerSecondMedian() const
{
return 1000000000.0 / RunTimeMedian();
}
/// 1st Quartile runs per second.
inline double RunsPerSecondQuartile1() const
{
return 1000000000.0 / RunTimeQuartile1();
}
/// 3rd Quartile runs per second.
inline double RunsPerSecondQuartile3() const
{
return 1000000000.0 / RunTimeQuartile3();
}
/// Maximum runs per second.
inline double RunsPerSecondMaximum() const
{
return 1000000000.0 / _timeRunMin;
}
/// Minimum runs per second.
inline double RunsPerSecondMinimum() const
{
return 1000000000.0 / _timeRunMax;
}
/// Average time per iteration.
inline double IterationTimeAverage() const
{
return RunTimeAverage() / double(_iterations);
}
/// Standard deviation time per iteration.
inline double IterationTimeStdDev() const
{
return RunTimeStdDev() / double(_iterations);
}
/// Median (2nd Quartile) time per iteration.
inline double IterationTimeMedian() const
{
return RunTimeMedian() / double(_iterations);
}
/// 1st Quartile time per iteration.
inline double IterationTimeQuartile1() const
{
return RunTimeQuartile1() / double(_iterations);
}
/// 3rd Quartile time per iteration.
inline double IterationTimeQuartile3() const
{
return RunTimeQuartile3() / double(_iterations);
}
/// Minimum time per iteration.
inline double IterationTimeMinimum() const
{
return _timeRunMin / double(_iterations);
}
/// Maximum time per iteration.
inline double IterationTimeMaximum() const
{
return _timeRunMax / double(_iterations);
}
/// Average iterations per second.
inline double IterationsPerSecondAverage() const
{
return 1000000000.0 / IterationTimeAverage();
}
/// Median (2nd Quartile) iterations per second.
inline double IterationsPerSecondMedian() const
{
return 1000000000.0 / IterationTimeMedian();
}
/// 1st Quartile iterations per second.
inline double IterationsPerSecondQuartile1() const
{
return 1000000000.0 / IterationTimeQuartile1();
}
/// 3rd Quartile iterations per second.
inline double IterationsPerSecondQuartile3() const
{
return 1000000000.0 / IterationTimeQuartile3();
}
/// Minimum iterations per second.
inline double IterationsPerSecondMinimum() const
{
return 1000000000.0 / IterationTimeMaximum();
}
/// Maximum iterations per second.
inline double IterationsPerSecondMaximum() const
{
return 1000000000.0 / IterationTimeMinimum();
}
private:
std::vector<uint64_t> _runTimes;
std::size_t _iterations;
uint64_t _timeTotal;
uint64_t _timeRunMin;
uint64_t _timeRunMax;
double _timeStdDev;
double _timeMedian;
double _timeQuartile1;
double _timeQuartile3;
};
}
#endif
|