-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
89 lines (80 loc) · 1.77 KB
/
main_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
package main
import (
"math/rand"
"sort"
"testing"
)
var denseArr []int
var spareArr []int
func BenchmarkSortSlice(b *testing.B) {
var testcases = []struct {
name string
input []int
}{
{name: "large array small values", input: denseArr},
{name: "large array large values", input: spareArr},
}
for _, t := range testcases {
b.Run(t.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
arr := make([]int, len(t.input))
copy(arr, t.input)
b.StartTimer()
sort.Slice(arr, func(i, j int) bool { return arr[i] < arr[j] })
}
})
}
}
func BenchmarkSortStable(b *testing.B) {
var testcases = []struct {
name string
input []int
}{
{name: "large array small values", input: denseArr},
{name: "large array large values", input: spareArr},
}
for _, t := range testcases {
b.Run(t.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
arr := make([]int, len(t.input))
copy(arr, t.input)
b.StartTimer()
sort.SliceStable(arr, func(i, j int) bool { return arr[i] < arr[j] })
}
})
}
}
func BenchmarkSortIndex(b *testing.B) {
var testcases = []struct {
name string
input []int
}{
{name: "large array small values", input: denseArr},
{name: "large array large values", input: spareArr},
}
for _, t := range testcases {
b.Run(t.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
arr := make([]int, len(t.input))
copy(arr, t.input)
b.StartTimer()
sort.Slice(arr, func(i, j int) bool {
if arr[i] == arr[j] {
return i < j
}
return arr[i] < arr[j]
})
}
})
}
}
func init() {
n := 500_000
for i := 0; i < n; i++ {
denseArr = append(denseArr, rand.Intn(1_000))
spareArr = append(spareArr, rand.Intn(1_000_000_000))
}
}