-
Notifications
You must be signed in to change notification settings - Fork 12
/
SpeedTest.cpp
142 lines (98 loc) · 2.65 KB
/
SpeedTest.cpp
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
#include "SpeedTest.h"
//-----------------------------------------------------------------------------
// We view our timing values as a series of random variables V that has been
// contaminated with occasional outliers due to cache misses, thread
// preemption, etcetera. To filter out the outliers, we search for the largest
// subset of V such that all its values are within three standard deviations
// of the mean.
double CalcMean ( std::vector<double> & v )
{
double mean = 0;
for(int i = 0; i < (int)v.size(); i++)
{
mean += v[i];
}
mean /= double(v.size());
return mean;
}
double CalcMean ( std::vector<double> & v, int a, int b )
{
double mean = 0;
for(int i = a; i <= b; i++)
{
mean += v[i];
}
mean /= (b-a+1);
return mean;
}
double CalcStdv ( std::vector<double> & v, int a, int b )
{
double mean = CalcMean(v,a,b);
double stdv = 0;
for(int i = a; i <= b; i++)
{
double x = v[i] - mean;
stdv += x*x;
}
stdv = sqrt(stdv / (b-a+1));
return stdv;
}
// Return true if the largest value in v[0,len) is more than three
// standard deviations from the mean
bool ContainsOutlier ( std::vector<double> & v, size_t len )
{
double mean = 0;
for(size_t i = 0; i < len; i++)
{
mean += v[i];
}
mean /= double(len);
double stdv = 0;
for(size_t i = 0; i < len; i++)
{
double x = v[i] - mean;
stdv += x*x;
}
stdv = sqrt(stdv / double(len));
double cutoff = mean + stdv*3;
return v[len-1] > cutoff;
}
// Do a binary search to find the largest subset of v that does not contain
// outliers.
void FilterOutliers ( std::vector<double> & v )
{
std::sort(v.begin(),v.end());
size_t len = 0;
for(size_t x = 0x40000000; x; x = x >> 1 )
{
if((len | x) >= v.size()) continue;
if(!ContainsOutlier(v,len | x))
{
len |= x;
}
}
v.resize(len);
}
// Iteratively tighten the set to find a subset that does not contain
// outliers. I'm not positive this works correctly in all cases.
void FilterOutliers2 ( std::vector<double> & v )
{
std::sort(v.begin(),v.end());
int a = 0;
int b = (int)(v.size() - 1);
for(int i = 0; i < 10; i++)
{
//printf("%d %d\n",a,b);
double mean = CalcMean(v,a,b);
double stdv = CalcStdv(v,a,b);
double cutA = mean - stdv*3;
double cutB = mean + stdv*3;
while((a < b) && (v[a] < cutA)) a++;
while((b > a) && (v[b] > cutB)) b--;
}
std::vector<double> v2;
v2.insert(v2.begin(),v.begin()+a,v.begin()+b+1);
v.swap(v2);
}
//-----------------------------------------------------------------------------
/* vim: set sts=2 sw=2 et: */