-
Notifications
You must be signed in to change notification settings - Fork 13
/
bench_test.go
283 lines (214 loc) · 5.47 KB
/
bench_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
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
package timsort
import (
"math/rand"
"sort"
"testing"
)
type record struct {
key, order int
}
type records []interface{}
func LessThanByKey(a, b interface{}) bool {
return a.(*record).key < b.(*record).key
}
type RecordSlice []record
func (s *RecordSlice) Len() int {
return len(*s)
}
func (s *RecordSlice) Swap(i, j int) {
(*s)[i], (*s)[j] = (*s)[j], (*s)[i]
}
func (s *RecordSlice) Less(i, j int) bool {
return (*s)[i].key < (*s)[j].key
}
func makeVector(size int, shape string) (v records) {
v = make(records, size)
switch shape {
case "xor":
for i := 0; i < size; i++ {
v[i] = &record{0xff & (i ^ 0xab), i}
}
case "sorted":
for i := 0; i < size; i++ {
v[i] = &record{i, i}
}
case "revsorted":
for i := 0; i < size; i++ {
v[i] = &record{size - i, i}
}
case "random":
rand.Seed(1)
for i := 0; i < size; i++ {
v[i] = &record{rand.Int(), i}
}
default:
panic(shape)
}
return v
}
func makeRecords(size int, shape string) (v RecordSlice) {
v = make(RecordSlice, size)
switch shape {
case "xor":
for i := 0; i < size; i++ {
v[i] = record{0xff & (i ^ 0xab), i}
}
case "sorted":
for i := 0; i < size; i++ {
v[i] = record{i, i}
}
case "revsorted":
for i := 0; i < size; i++ {
v[i] = record{size - i, i}
}
case "random":
rand.Seed(1)
for i := 0; i < size; i++ {
v[i] = record{rand.Int(), i}
}
default:
panic(shape)
}
return v
}
func benchmarkTimsort(b *testing.B, size int, shape string) {
b.StopTimer()
for j := 0; j < b.N; j++ {
v := makeVector(size, shape)
b.StartTimer()
err := Sort(v, LessThanByKey)
b.StopTimer()
if err != nil {
b.Fatalf("Sort: %v", err)
}
}
}
func benchmarkTimsortInterface(b *testing.B, size int, shape string) {
b.StopTimer()
for j := 0; j < b.N; j++ {
v := makeRecords(size, shape)
b.StartTimer()
err := TimSort(&v)
b.StopTimer()
if err != nil {
b.Fatalf("TimSort: %v", err)
}
}
}
func benchmarkStandardSort(b *testing.B, size int, shape string) {
b.StopTimer()
for j := 0; j < b.N; j++ {
v := makeRecords(size, shape)
b.StartTimer()
sort.Stable(&v)
b.StopTimer()
}
}
func BenchmarkTimsortXor100(b *testing.B) {
benchmarkTimsort(b, 100, "xor")
}
func BenchmarkTimsortInterXor100(b *testing.B) {
benchmarkTimsortInterface(b, 100, "xor")
}
func BenchmarkStandardSortXor100(b *testing.B) {
benchmarkStandardSort(b, 100, "xor")
}
func BenchmarkTimsortSorted100(b *testing.B) {
benchmarkTimsort(b, 100, "sorted")
}
func BenchmarkTimsortInterSorted100(b *testing.B) {
benchmarkTimsortInterface(b, 100, "sorted")
}
func BenchmarkStandardSortSorted100(b *testing.B) {
benchmarkStandardSort(b, 100, "sorted")
}
func BenchmarkTimsortRevSorted100(b *testing.B) {
benchmarkTimsort(b, 100, "revsorted")
}
func BenchmarkTimsortInterRevSorted100(b *testing.B) {
benchmarkTimsortInterface(b, 100, "revsorted")
}
func BenchmarkStandardSortRevSorted100(b *testing.B) {
benchmarkStandardSort(b, 100, "revsorted")
}
func BenchmarkTimsortRandom100(b *testing.B) {
benchmarkTimsort(b, 100, "random")
}
func BenchmarkTimsortInterRandom100(b *testing.B) {
benchmarkTimsortInterface(b, 100, "random")
}
func BenchmarkStandardSortRandom100(b *testing.B) {
benchmarkStandardSort(b, 100, "random")
}
func BenchmarkTimsortXor1K(b *testing.B) {
benchmarkTimsort(b, 1024, "xor")
}
func BenchmarkTimsortInterXor1K(b *testing.B) {
benchmarkTimsortInterface(b, 1024, "xor")
}
func BenchmarkStandardSortXor1K(b *testing.B) {
benchmarkStandardSort(b, 1024, "xor")
}
func BenchmarkTimsortSorted1K(b *testing.B) {
benchmarkTimsort(b, 1024, "sorted")
}
func BenchmarkTimsortInterSorted1K(b *testing.B) {
benchmarkTimsortInterface(b, 1024, "sorted")
}
func BenchmarkStandardSortSorted1K(b *testing.B) {
benchmarkStandardSort(b, 1024, "sorted")
}
func BenchmarkTimsortRevSorted1K(b *testing.B) {
benchmarkTimsort(b, 1024, "revsorted")
}
func BenchmarkTimsortInterRevSorted1K(b *testing.B) {
benchmarkTimsortInterface(b, 1024, "revsorted")
}
func BenchmarkStandardSortRevSorted1K(b *testing.B) {
benchmarkStandardSort(b, 1024, "revsorted")
}
func BenchmarkTimsortRandom1K(b *testing.B) {
benchmarkTimsort(b, 1024, "random")
}
func BenchmarkTimsortInterRandom1K(b *testing.B) {
benchmarkTimsortInterface(b, 1024, "random")
}
func BenchmarkStandardSortRandom1K(b *testing.B) {
benchmarkStandardSort(b, 1024, "random")
}
func BenchmarkTimsortXor1M(b *testing.B) {
benchmarkTimsort(b, 1024*1024, "xor")
}
func BenchmarkTimsortInterXor1M(b *testing.B) {
benchmarkTimsortInterface(b, 1024*1024, "xor")
}
func BenchmarkStandardSortXor1M(b *testing.B) {
benchmarkStandardSort(b, 1024*1024, "xor")
}
func BenchmarkTimsortSorted1M(b *testing.B) {
benchmarkTimsort(b, 1024*1024, "sorted")
}
func BenchmarkTimsortInterSorted1M(b *testing.B) {
benchmarkTimsortInterface(b, 1024*1024, "sorted")
}
func BenchmarkStandardSortSorted1M(b *testing.B) {
benchmarkStandardSort(b, 1024*1024, "sorted")
}
func BenchmarkTimsortRevSorted1M(b *testing.B) {
benchmarkTimsort(b, 1024*1024, "revsorted")
}
func BenchmarkTimsortInterRevSorted1M(b *testing.B) {
benchmarkTimsortInterface(b, 1024*1024, "revsorted")
}
func BenchmarkStandardSortRevSorted1M(b *testing.B) {
benchmarkStandardSort(b, 1024*1024, "revsorted")
}
func BenchmarkTimsortRandom1M(b *testing.B) {
benchmarkTimsort(b, 1024*1024, "random")
}
func BenchmarkTimsortInterRandom1M(b *testing.B) {
benchmarkTimsortInterface(b, 1024*1024, "random")
}
func BenchmarkStandardSortRandom1M(b *testing.B) {
benchmarkStandardSort(b, 1024*1024, "random")
}