-
Notifications
You must be signed in to change notification settings - Fork 17
/
window_test.go
147 lines (128 loc) · 2.79 KB
/
window_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
package window
import "testing"
import "container/list"
import "container/ring"
func BenchmarkListS1000(b *testing.B) {
l(1000,b)
}
func BenchmarkRingS1000(b *testing.B) {
r(1000,b)
}
func BenchmarkS1000M1(b *testing.B) {
m(1000,1,b)
}
func BenchmarkS1000M10(b *testing.B) {
m(1000,10,b)
}
func BenchmarkS1000M100(b *testing.B) {
m(1000,100,b)
}
func BenchmarkS1000M500(b *testing.B) {
m(1000,500,b)
}
func BenchmarkSlicifyList(b *testing.B) {
b.StopTimer()
l := getList(1000)
b.StartTimer()
for i := 0; i < b.N; i++ {
slicifyList(l)
}
}
func BenchmarkSlicifyRing(b *testing.B) {
b.StopTimer()
l := getRing(1000)
b.StartTimer()
for i := 0; i < b.N; i++ {
slicifyRing(l)
}
}
func BenchmarkSlicifyWindow(b *testing.B) {
b.StopTimer()
w := getWindow(1000)
b.StartTimer()
for i := 0; i < b.N; i++ {
slicifyWindow(w)
}
}
// m will, given the size and multiple, run
// 1000 times the size worth of data through
// the moving window
func m(size, multiple int, b *testing.B) {
b.StopTimer()
w := New(size, multiple)
TIMES := 1000*size
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < TIMES; j++ {
w.PushBack(float64(i))
}
}
}
func l(size int, b *testing.B) {
b.StopTimer()
lst := list.New()
TIMES := 1000*size
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < TIMES; j++ {
lst.PushBack(float64(j))
if (lst.Len() > size) {
lst.Remove(lst.Front())
}
}
}
}
// contributed by: Dustin
func r(size int, b *testing.B) {
b.StopTimer()
rng := ring.New(size)
TIMES := 1000 * size
b.StartTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < TIMES; j++ {
rng.Value = float64(j)
rng = rng.Prev()
}
}
}
func getList(size int) (l *list.List){
l = list.New()
for i := 0; i < size; i++ {
l.PushBack(float64(i))
}
return
}
func getRing(size int) (r *ring.Ring) {
r = ring.New(size)
for i := 0; i < size; i++ {
r.Value = float64(i)
r = r.Prev()
}
return
}
func getWindow(size int) (w *MovingWindow) {
w = New(size, 1)
for i := 0; i < size; i++ {
w.PushBack(float64(i))
}
return
}
func slicifyList(lst *list.List) {
s := make([]float64, 0, lst.Len())
for e := lst.Front(); e != nil; e = e.Next() {
s = append(s, e.Value.(float64))
}
}
func slicifyRing(r *ring.Ring) {
l := r.Len()
s := make([]float64, 0, l)
for i := 0; i < l; i++ {
s = append(s, r.Value.(float64))
r = r.Prev()
}
}
// not necessary, but to be completely fair
func slicifyWindow(w *MovingWindow) {
s := w.Slice()
s[0] = 0
}