-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_darwin_test.go
193 lines (152 loc) · 5.28 KB
/
stats_darwin_test.go
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
// +build darwin,cgo
package main
import (
"testing"
"time"
)
func CalculateVarianceInGo(values []uint32) float64 {
var sum uint32 = 0
for _, value := range values {
sum += value
}
mean := float64(sum) / float64(len(values))
var accumulator float64
for _, value := range values {
diff := float64(value) - mean
accumulator += diff * diff
}
return accumulator / float64(len(values)-1)
}
func Test_CalculateVariance_4096_SpeedTest(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
return
}
var values = make([]uint32, 4096, 4096)
start := time.Now()
for i := 0; i < 100_000; i++ {
CalculateVarianceInGo(values)
}
duration := time.Since(start)
t.Logf("Spent %s calling go code with vector of size 4096 64-bit floats", duration)
}
func Test_CalculateVarianceInGo_SpeedTest(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
return
}
var values = make([]uint32, 512, 512)
start := time.Now()
for i := 0; i < 100_000; i++ {
CalculateVarianceInGo(values)
}
duration := time.Since(start)
t.Logf("Spent %s running calling go code with vector of size 512 64-bit floats", duration)
}
func Test_CalculateVarianceInGo_64_SpeedTest(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
return
}
var values = make([]uint32, 64, 64)
start := time.Now()
for i := 0; i < 100_000; i++ {
CalculateVarianceInGo(values)
}
duration := time.Since(start)
t.Logf("Spent %s running calling go code with vector of size 64 64-bit floats", duration)
}
func Test_CalculateVariance_4096_SpeedTestOnDarwin(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
return
}
var values = make([]uint32, 4096, 4096)
start := time.Now()
for i := 0; i < 100_000; i++ {
CalculateVariance(values)
}
duration := time.Since(start)
t.Logf(`Spent %s calling c code with vector of size 4096 64-bit floats.
Utilizing Apple Accelerate Framwork. Using CGO Foreign Function Interface (FFI).`, duration)
}
func Test_CalculateVariance_SpeedTestOnDarwin(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
return
}
var values = make([]uint32, 512, 512)
start := time.Now()
for i := 0; i < 100_000; i++ {
CalculateVariance(values)
}
duration := time.Since(start)
t.Logf(`Spent %s calling c code with vector of size 512 64-bit floats.
Utilizing Apple Accelerate Framwork. Using CGO Foreign Function Interface (FFI).`, duration)
}
func Test_CalculateVariance_64_SpeedTestOnDarwin(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
return
}
var values = make([]uint32, 64, 64)
start := time.Now()
for i := 0; i < 100_000; i++ {
CalculateVariance(values)
}
duration := time.Since(start)
t.Logf(`Spent %s calling c code with vector of size 64 64-bit floats.
Utilizing Apple Accelerate Framwork. Using CGO Foreign Function Interface (FFI).`, duration)
}
func Test_CalculateVarianceInGo_InputIsOneOfFortySix(t *testing.T) {
var values = []uint32{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
result := CalculateVarianceInGo(values)
// 0.021266540642722
if result < 0.02173913043478260 {
t.Fatalf("Variance is to small. got %.18f. Calling portable go function.", result)
}
if result > 0.02173913043478261 {
t.Fatalf("Variance is to big. got %.18f. Calling portable go function.", result)
}
}
func Test_CalculateVariance_InputIsOneOfFortySix(t *testing.T) {
var values = []uint32{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
result := CalculateVariance(values)
// 0.021266540642722
if result < 0.02173913043478260 {
t.Fatalf("Variance is to small. Expected 0.021739130434782608. got %.18f. Utilizing Apple Accelerate Framework throught CGO.", result)
}
if result > 0.02173913043478261 {
t.Fatalf("Variance is to big. Expected 0.021739130434782608. got %.18f. Utilizing Apple Accelerate Framework throught CGO.", result)
}
}
func Test_CalculateVariance_InputHasEvenNumberOfValues(t *testing.T) {
var values = []uint32{1, 2, 1, 2}
result := CalculateVariance(values)
if result < 0.33333 {
t.Fatalf("Variance is to small. expected 0.333... got %f. Utilizing Apple Accelerate Framework throught CGO.", result)
}
if result > 0.33334 {
t.Fatalf("Variance is to big. expected 0.333... got %f. Utilizing Apple Accelerate Framework throught CGO.", result)
}
}
func Test_CalculateVarianceInGo_InputHasEvenNumberOfValues(t *testing.T) {
var values = []uint32{1, 2, 1, 2}
result := CalculateVarianceInGo(values)
if result < 0.33333 {
t.Fatalf("Variance is to small. expected 0.333... got %f. Calling portable go function.", result)
}
if result > 0.33334 {
t.Fatalf("Variance is to big. expected 0.333... got %f. Calling portable go function.", result)
}
}
func Test_CalculateVariance_InputHasOddNumberOfValues(t *testing.T) {
var values = []uint32{1, 2, 1, 2, 1}
result := CalculateVariance(values)
if result < 0.3 {
t.Fatalf("Variance is to small. got %f. Utilizing Apple Accelerate Framework throught CGO.", result)
}
if result > 0.3 {
t.Fatalf("Variance is to big. got %f. Utilizing Apple Accelerate Framework throught CGO.", result)
}
}