Skip to content

Commit 1bc9a4f

Browse files
elibeneric
authored andcommitted
slices: add benchmark for IsSorted vs. IntsAreSorted
We'd like to mention in a comment that users should prefer slices.IsSorted over sort.IntsAreSorted and similar functions. Create a benchmark that shows this. goos: linux goarch: amd64 pkg: slices cpu: 11th Gen Intel(R) Core(TM) i5-1135G7 @ 2.40GHz BenchmarkIntsAreSorted-8 6031 198315 ns/op BenchmarkIsSorted-8 26580 45801 ns/op Change-Id: I4f14fafd799ecec35c8a5215b74994e972103061 Reviewed-on: https://go-review.googlesource.com/c/go/+/502556 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Eli Bendersky‎ <[email protected]> Reviewed-by: Eli Bendersky <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 54d1fa3 commit 1bc9a4f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/slices/sort_benchmark_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ func BenchmarkSlicesSortInts_Reversed(b *testing.B) {
7777
}
7878
}
7979

80+
func BenchmarkIntsAreSorted(b *testing.B) {
81+
for i := 0; i < b.N; i++ {
82+
b.StopTimer()
83+
ints := makeSortedInts(N)
84+
b.StartTimer()
85+
sort.IntsAreSorted(ints)
86+
}
87+
}
88+
89+
func BenchmarkIsSorted(b *testing.B) {
90+
for i := 0; i < b.N; i++ {
91+
b.StopTimer()
92+
ints := makeSortedInts(N)
93+
b.StartTimer()
94+
IsSorted(ints)
95+
}
96+
}
97+
8098
// Since we're benchmarking these sorts against each other, make sure that they
8199
// generate similar results.
82100
func TestIntSorts(t *testing.T) {

0 commit comments

Comments
 (0)