forked from pgmmpk/timsort
/
bench_test.go
189 lines (142 loc) · 3.53 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
package timsort
import (
"testing"
"sort"
"rand"
// "fmt"
"container/vector"
)
type record struct {
key, order int
}
func (self *record) Less(o interface{}) bool {
return self.key < o.(*record).key
}
func LessThanByKey(a, b interface{}) bool {
return a.(*record).key < b.(*record).key
}
func LessThanByKeyByOrder(a, b interface{}) bool {
aa := a.(*record)
bb := b.(*record)
if aa.key < bb.key {
return true
} else if aa.key == bb.key {
return aa.order < bb.order
}
return false
}
func makeVector(size int, shape string) vector.Vector {
var v vector.Vector
switch shape {
case "xor":
for i := 0; i < size; i++ {
v.Push(&record{0xff & (i ^ 0xab), i})
}
case "sorted":
for i := 0; i < size; i++ {
v.Push(&record{i, i})
}
case "revsorted":
for i := 0; i < size; i++ {
v.Push(&record{size - i, i})
}
case "random":
rand.Seed(1)
for i := 0; i < size; i++ {
v.Push(&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()
Sort(v, LessThanByKey)
b.StopTimer()
}
}
func benchmarkStandardSort(b *testing.B, size int, shape string) {
b.StopTimer()
for j := 0; j < b.N; j++ {
v := makeVector(size, shape)
b.StartTimer()
sort.Sort(&v)
b.StopTimer()
}
}
func BenchmarkTimsortXor100(b *testing.B) {
benchmarkTimsort(b, 100, "xor")
}
func BenchmarkStandardSortXor100(b *testing.B) {
benchmarkStandardSort(b, 100, "xor")
}
func BenchmarkTimsortSorted100(b *testing.B) {
benchmarkTimsort(b, 100, "sorted")
}
func BenchmarkStandardSortSorted100(b *testing.B) {
benchmarkStandardSort(b, 100, "sorted")
}
func BenchmarkTimsortRevSorted100(b *testing.B) {
benchmarkTimsort(b, 100, "revsorted")
}
func BenchmarkStandardSortRevSorted100(b *testing.B) {
benchmarkStandardSort(b, 100, "revsorted")
}
func BenchmarkTimsortRandom100(b *testing.B) {
benchmarkTimsort(b, 100, "random")
}
func BenchmarkStandardSortRandom100(b *testing.B) {
benchmarkStandardSort(b, 100, "random")
}
func BenchmarkTimsortXor1K(b *testing.B) {
benchmarkTimsort(b, 1024, "xor")
}
func BenchmarkStandardSortXor1K(b *testing.B) {
benchmarkStandardSort(b, 1024, "xor")
}
func BenchmarkTimsortSorted1K(b *testing.B) {
benchmarkTimsort(b, 1024, "sorted")
}
func BenchmarkStandardSortSorted1K(b *testing.B) {
benchmarkStandardSort(b, 1024, "sorted")
}
func BenchmarkTimsortRevSorted1K(b *testing.B) {
benchmarkTimsort(b, 1024, "revsorted")
}
func BenchmarkStandardSortRevSorted1K(b *testing.B) {
benchmarkStandardSort(b, 1024, "revsorted")
}
func BenchmarkTimsortRandom1K(b *testing.B) {
benchmarkTimsort(b, 1024, "random")
}
func BenchmarkStandardSortRandom1K(b *testing.B) {
benchmarkStandardSort(b, 1024, "random")
}
func BenchmarkTimsortXor1M(b *testing.B) {
benchmarkTimsort(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 BenchmarkStandardSortSorted1M(b *testing.B) {
benchmarkStandardSort(b, 1024*1024, "sorted")
}
func BenchmarkTimsortRevSorted1M(b *testing.B) {
benchmarkTimsort(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 BenchmarkStandardSortRandom1M(b *testing.B) {
benchmarkStandardSort(b, 1024*1024, "random")
}