"fmt"
"math/rand"
"sort"
+ "strconv"
"strings"
"testing"
)
}
}
+func makeSortedStrings(n int) []string {
+ x := make([]string, n)
+ for i := 0; i < n; i++ {
+ x[i] = strconv.Itoa(i)
+ }
+ Sort(x)
+ return x
+}
+
func BenchmarkSlicesSortInts(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
}
}
+func BenchmarkSortStrings_Sorted(b *testing.B) {
+ ss := makeSortedStrings(N)
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ sort.Strings(ss)
+ }
+}
+
func BenchmarkSlicesSortStrings(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
}
}
+func BenchmarkSlicesSortStrings_Sorted(b *testing.B) {
+ ss := makeSortedStrings(N)
+ b.ResetTimer()
+
+ for i := 0; i < b.N; i++ {
+ Sort(ss)
+ }
+}
+
// These benchmarks compare sorting a slice of structs with sort.Sort vs.
// slices.SortFunc.
type myStruct struct {