-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_test.go
93 lines (82 loc) · 1.71 KB
/
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
package sort
import (
"fmt"
"math/rand"
"runtime"
"sort"
"testing"
)
func BenchmarkSortGenerics(b *testing.B) {
for _, size := range []uint{1, 10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
rng := rand.New(rand.NewSource(0))
slices := 1000/size + 30
vs := make([][]int, slices)
for idx := range vs {
vs[idx] = make([]int, size)
s := vs[idx]
for idx := range s {
s[idx] = rng.Intn(int(size)/2 + 1)
}
}
cs := make([]intSlice, slices)
for idx := range cs {
cs[idx] = make([]int, size)
}
runtime.GC()
runtime.GC()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
idx := uint(i) % slices
if idx == 0 {
b.StopTimer()
for idx := range cs {
copy(cs[idx], vs[idx])
}
b.StartTimer()
}
Sort(cs[idx])
}
})
}
}
func BenchmarkStdSort(b *testing.B) {
for _, size := range []uint{1, 10, 100, 1000, 10000, 100000} {
b.Run(fmt.Sprintf("%d", size), func(b *testing.B) {
rng := rand.New(rand.NewSource(0))
slices := 1000/size + 30
vs := make([][]int, slices)
for idx := range vs {
vs[idx] = make([]int, size)
s := vs[idx]
for idx := range s {
s[idx] = rng.Intn(int(size)/2 + 1)
}
}
cs := make([]sort.IntSlice, slices)
for idx := range cs {
cs[idx] = make([]int, size)
}
runtime.GC()
runtime.GC()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
idx := uint(i) % slices
if idx == 0 {
b.StopTimer()
for idx := range cs {
copy(cs[idx], vs[idx])
}
b.StartTimer()
}
sort.Sort(cs[idx])
}
})
}
}
type intSlice []int
func (s intSlice) Less(i, j int) bool {
return s[i] < s[j]
}