]> Cypherpunks.ru repositories - gostls13.git/commitdiff
slices: add sort benchmark for sorted strings
authorEli Bendersky <eliben@golang.org>
Thu, 15 Jun 2023 16:34:22 +0000 (09:34 -0700)
committerEli Bendersky <eliben@google.com>
Thu, 15 Jun 2023 16:58:05 +0000 (16:58 +0000)
For #60777

Change-Id: I424535ce6454156c61af2f299228630ee304d165
Reviewed-on: https://go-review.googlesource.com/c/go/+/503815
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Eli Bendersky <eliben@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Eli Bendersky <eliben@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/slices/sort_benchmark_test.go

index edf29994cf3443b3772179bf11795a57f5828256..0f088425949df31a4b70c3992f29469c546f4086 100644 (file)
@@ -8,6 +8,7 @@ import (
        "fmt"
        "math/rand"
        "sort"
+       "strconv"
        "strings"
        "testing"
 )
@@ -50,6 +51,15 @@ func BenchmarkSortInts(b *testing.B) {
        }
 }
 
+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()
@@ -153,6 +163,15 @@ func BenchmarkSortStrings(b *testing.B) {
        }
 }
 
+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()
@@ -162,6 +181,15 @@ func BenchmarkSlicesSortStrings(b *testing.B) {
        }
 }
 
+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 {