File tree 1 file changed +28
-0
lines changed
1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 8
8
"fmt"
9
9
"math/rand"
10
10
"sort"
11
+ "strconv"
11
12
"strings"
12
13
"testing"
13
14
)
@@ -50,6 +51,15 @@ func BenchmarkSortInts(b *testing.B) {
50
51
}
51
52
}
52
53
54
+ func makeSortedStrings (n int ) []string {
55
+ x := make ([]string , n )
56
+ for i := 0 ; i < n ; i ++ {
57
+ x [i ] = strconv .Itoa (i )
58
+ }
59
+ Sort (x )
60
+ return x
61
+ }
62
+
53
63
func BenchmarkSlicesSortInts (b * testing.B ) {
54
64
for i := 0 ; i < b .N ; i ++ {
55
65
b .StopTimer ()
@@ -153,6 +163,15 @@ func BenchmarkSortStrings(b *testing.B) {
153
163
}
154
164
}
155
165
166
+ func BenchmarkSortStrings_Sorted (b * testing.B ) {
167
+ ss := makeSortedStrings (N )
168
+ b .ResetTimer ()
169
+
170
+ for i := 0 ; i < b .N ; i ++ {
171
+ sort .Strings (ss )
172
+ }
173
+ }
174
+
156
175
func BenchmarkSlicesSortStrings (b * testing.B ) {
157
176
for i := 0 ; i < b .N ; i ++ {
158
177
b .StopTimer ()
@@ -162,6 +181,15 @@ func BenchmarkSlicesSortStrings(b *testing.B) {
162
181
}
163
182
}
164
183
184
+ func BenchmarkSlicesSortStrings_Sorted (b * testing.B ) {
185
+ ss := makeSortedStrings (N )
186
+ b .ResetTimer ()
187
+
188
+ for i := 0 ; i < b .N ; i ++ {
189
+ Sort (ss )
190
+ }
191
+ }
192
+
165
193
// These benchmarks compare sorting a slice of structs with sort.Sort vs.
166
194
// slices.SortFunc.
167
195
type myStruct struct {
You can’t perform that action at this time.
0 commit comments