Skip to content

Commit f6e0dcc

Browse files
committed
slices: add sort benchmark for sorted strings
For #60777 Change-Id: I424535ce6454156c61af2f299228630ee304d165 Reviewed-on: https://go-review.googlesource.com/c/go/+/503815 Reviewed-by: Keith Randall <[email protected]> Run-TryBot: Eli Bendersky <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Eli Bendersky <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent b7e7467 commit f6e0dcc

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/slices/sort_benchmark_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"math/rand"
1010
"sort"
11+
"strconv"
1112
"strings"
1213
"testing"
1314
)
@@ -50,6 +51,15 @@ func BenchmarkSortInts(b *testing.B) {
5051
}
5152
}
5253

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+
5363
func BenchmarkSlicesSortInts(b *testing.B) {
5464
for i := 0; i < b.N; i++ {
5565
b.StopTimer()
@@ -153,6 +163,15 @@ func BenchmarkSortStrings(b *testing.B) {
153163
}
154164
}
155165

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+
156175
func BenchmarkSlicesSortStrings(b *testing.B) {
157176
for i := 0; i < b.N; i++ {
158177
b.StopTimer()
@@ -162,6 +181,15 @@ func BenchmarkSlicesSortStrings(b *testing.B) {
162181
}
163182
}
164183

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+
165193
// These benchmarks compare sorting a slice of structs with sort.Sort vs.
166194
// slices.SortFunc.
167195
type myStruct struct {

0 commit comments

Comments
 (0)