-
Notifications
You must be signed in to change notification settings - Fork 94
/
bench_sort_test.go
292 lines (241 loc) · 6.1 KB
/
bench_sort_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
284
285
286
287
288
289
290
291
292
/*
* gomacro - A Go interpreter with Lisp-like macros
*
* Copyright (C) 2018-2019 Massimiliano Ghilardi
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*
* bench_sort_test.go
*
* Created on: Jun 09 2018
* Author: Massimiliano Ghilardi
*/
package main
import (
"fmt"
"math/rand"
"sort"
"testing"
"github.com/cosmos72/gomacro/classic"
"github.com/cosmos72/gomacro/fast"
)
var verbosesort = verbose
func quicksortInts(v []int) {
for len(v) >= 30 {
n := len(v)
left, right := 0, n-1
pivot := rand.Intn(n)
v[pivot], v[right] = v[right], v[pivot]
for i, _ := range v {
if v[i] < v[right] {
v[left], v[i] = v[i], v[left]
left++
}
}
v[left], v[right] = v[right], v[left]
if left < n-left {
quicksortInts(v[:left])
v = v[left+1:]
} else {
quicksortInts(v[left+1:])
v = v[:left]
}
}
if n := len(v); n >= 3 {
shellsortInts(v)
} else if n == 2 {
if v[0] > v[1] {
v[0], v[1] = v[1], v[0]
}
}
}
// ---------------------- arrays: shellsort ------------------------
// array indexing is faster that slice indexing,
// provided the array is *not* copied. so use a pointer to array
var shellshort_gaps = &[...]int{8839, 3797, 1631, 701, 301, 132, 57, 23, 10, 4, 1}
func shellsortInts(v []int) {
var i, j, n, gap, temp int
n = len(v)
for _, gap = range shellshort_gaps {
for i = gap; i < n; i++ {
temp = v[i]
for j = i; j >= gap && v[j-gap] > temp; j -= gap {
v[j] = v[j-gap]
}
v[j] = temp
}
}
}
func shellsortIntSlice(ints []int) {
var v sort.IntSlice = ints
var i, j, n, gap int
n = v.Len()
for _, gap = range shellshort_gaps {
for i = gap; i < n; i++ {
for j = i; j >= gap && v.Less(j, j-gap); j -= gap {
v.Swap(j, j-gap)
}
}
}
}
func shellsortInterfaces(ints []int) {
// if v is declared with type sort.IntSlice, performance reaches shellsortInts() above
var v sort.Interface = sort.IntSlice(ints)
var i, j, n, gap int
n = v.Len()
for _, gap = range shellshort_gaps {
for i = gap; i < n; i++ {
for j = i; j >= gap && v.Less(j, j-gap); j -= gap {
v.Swap(j, j-gap)
}
}
}
}
const shellsort_ints_source_string = `
var shellshort_gaps = [...]int{8839, 3797, 1631, 701, 301, 132, 57, 23, 10, 4, 1}
func shellsort(v []int) {
var i, j, n, temp int
n = len(v)
for _, gap := range shellshort_gaps {
for i = gap; i < n; i++ {
temp = v[i]
for j = i; j >= gap && v[j-gap] > temp; j -= gap {
v[j] = v[j-gap]
}
v[j] = temp
}
}
}`
const shellsort_intslice_source_string = `
import "sort"
var shellshort_gaps = [...]int{8839, 3797, 1631, 701, 301, 132, 57, 23, 10, 4, 1}
func shellsort(ints []int) {
var v sort.IntSlice = ints
var i, j, n, gap int
n = v.Len()
for _, gap = range shellshort_gaps {
for i = gap; i < n; i++ {
for j = i; j >= gap && v.Less(j, j-gap); j -= gap {
v.Swap(j, j-gap)
}
}
}
}`
const shellsort_interfaces_source_string = `
import "sort"
var shellshort_gaps = [...]int{8839, 3797, 1631, 701, 301, 132, 57, 23, 10, 4, 1}
func shellsort(ints []int) {
var v sort.Interface = sort.IntSlice(ints)
var i, j, n, gap int
n = v.Len()
for _, gap = range shellshort_gaps {
for i = gap; i < n; i++ {
for j = i; j >= gap && v.Less(j, j-gap); j -= gap {
v.Swap(j, j-gap)
}
}
}
}`
var shellsort_generic_source_string = `
var shellshort_gaps = [...]int{8839, 3797, 1631, 701, 301, 132, 57, 23, 10, 4, 1}
` + generic_func("shellsort", "T") + ` (v []T) {
var i, j, n int
var temp T
n = len(v)
for _, gap := range shellshort_gaps {
for i = gap; i < n; i++ {
temp = v[i]
for j = i; j >= gap && v[j-gap] > temp; j -= gap {
v[j] = v[j-gap]
}
v[j] = temp
}
}
}`
func BenchmarkQuickSortCompilerInts(b *testing.B) {
benchmark_sort(b, quicksortInts)
}
func BenchmarkShellSortCompilerInts(b *testing.B) {
benchmark_sort(b, shellsortInts)
}
func BenchmarkShellSortCompilerIntSlice(b *testing.B) {
benchmark_sort(b, shellsortIntSlice)
}
func BenchmarkShellSortCompilerInterfaces(b *testing.B) {
benchmark_sort(b, shellsortInterfaces)
}
func BenchmarkStdSortCompilerInterfaces(b *testing.B) {
// use standard library sort
benchmark_sort(b, sort.Ints)
}
func BenchmarkShellSortFastInts(b *testing.B) {
ir := fast.New()
ir.Eval(shellsort_ints_source_string)
// extract the function shellsort()
sort := ir.ValueOf("shellsort").Interface().(func([]int))
benchmark_sort(b, sort)
}
func BenchmarkShellSortFastGeneric(b *testing.B) {
ir := fast.New()
ir.Eval(shellsort_generic_source_string)
// extract the function shellsort#[int]()
vs, _ := ir.Eval("shellsort#[int]")
sort := vs[0].Interface().(func([]int))
benchmark_sort(b, sort)
}
func BenchmarkShellSortFastInterfaces(b *testing.B) {
ir := fast.New()
ir.Eval(shellsort_interfaces_source_string)
// extract the function shellsort()
sort := ir.ValueOf("shellsort").Interface().(func([]int))
benchmark_sort(b, sort)
}
func BenchmarkShellSortFastIntSlice(b *testing.B) {
ir := fast.New()
ir.Eval(shellsort_intslice_source_string)
// extract the function shellsort()
sort := ir.ValueOf("shellsort").Interface().(func([]int))
benchmark_sort(b, sort)
}
func BenchmarkShellSortFastCompileLoop(b *testing.B) {
ir := fast.New()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ir.Comp.Binds = make(map[string]*fast.Bind)
ir.Comp.BindNum = fast.NoIndex
ir.Compile(shellsort_ints_source_string)
}
}
func BenchmarkShellSortClassicInts(b *testing.B) {
ir := classic.New()
ir.Eval(shellsort_ints_source_string)
// extract the function shellsort()
sort := ir.ValueOf("shellsort").Interface().(func([]int))
benchmark_sort(b, sort)
}
var sort_data = make_sort_data()
func make_sort_data() []int {
const n = 10000
v := make([]int, n)
for i := 0; i < n; i++ {
v[i] = rand.Int()
}
return v
}
func benchmark_sort(b *testing.B, sort func([]int)) {
// call sort once for warm-up
v := make([]int, len(sort_data))
copy(v, sort_data)
sort(v)
b.ResetTimer()
for i := 0; i < b.N; i++ {
copy(v, sort_data)
sort(v)
}
if verbosesort {
fmt.Println(v)
}
}