diff options
author | Eli Bendersky <eliben@golang.org> | 2023-06-12 09:05:33 -0700 |
---|---|---|
committer | Eli Bendersky <eliben@google.com> | 2023-06-13 12:26:08 +0000 |
commit | d9b87d7cc3281a3d3051dfa75e30251f25ee117d (patch) | |
tree | f914013d309be613f908677e1b1d59a07eb853c6 | |
parent | b0e1707f94b9c6254c8cb2987d7b5c0db2ec5f2e (diff) | |
download | go-d9b87d7cc3281a3d3051dfa75e30251f25ee117d.tar.gz go-d9b87d7cc3281a3d3051dfa75e30251f25ee117d.zip |
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 <iant@google.com>
Run-TryBot: Eli Benderskyā€ˇ <eliben@golang.org>
Reviewed-by: Eli Bendersky <eliben@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
-rw-r--r-- | src/slices/sort_benchmark_test.go | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/src/slices/sort_benchmark_test.go b/src/slices/sort_benchmark_test.go index 88eb2385de..edf29994cf 100644 --- a/src/slices/sort_benchmark_test.go +++ b/src/slices/sort_benchmark_test.go @@ -77,6 +77,24 @@ func BenchmarkSlicesSortInts_Reversed(b *testing.B) { } } +func BenchmarkIntsAreSorted(b *testing.B) { + for i := 0; i < b.N; i++ { + b.StopTimer() + ints := makeSortedInts(N) + b.StartTimer() + sort.IntsAreSorted(ints) + } +} + +func BenchmarkIsSorted(b *testing.B) { + for i := 0; i < b.N; i++ { + b.StopTimer() + ints := makeSortedInts(N) + b.StartTimer() + IsSorted(ints) + } +} + // Since we're benchmarking these sorts against each other, make sure that they // generate similar results. func TestIntSorts(t *testing.T) { |