-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sort: slices.Sort is slightly slower than sort.Strings on some sorted inputs #60777
Comments
Which document are you referring to? |
CC @eliben |
That's not a great benchmark, because it just calls the sorting function over and over again on the same slice. After the first call, the slice will be fully sorted. So you aren't really testing how long it takes to sort, you are testing how long it takes to determine that the slice is already sorted. There are some existing benchmarks in slices/sort_benchmark_test.go and sort/sort_test.go that don't have that problem. |
Document in https://pkg.go.dev/sort@master#Strings codepackage mainimport ( const n = 100000 func BenchmarkStringSort(b *testing.B) { // func BenchmarkStringSort(b *testing.B) {
│ sort.txt │ slices.txt │
│ sec/op │ sec/op vs base │
StringSort-4 38.63m ± 1% 36.23m ± 2% -6.23% (p=0.000 n=10)
Note: consider using the new slides. Sort function, which runs faster This document is easily understood as calling slices.Sort on the same slice (using [] string as an example) is faster than calling sort.Strings. I think this part of the document should be changed to: "Note: Consider using the newer slices.Sort function to run faster on Not sorted slices", It's much less likely to be misunderstood. |
I think author means "documentation". |
In my measurements, Both functions end up in the same code-generated implementation, and the difference between them is the cost of dispatch ( |
"IMOP, we can optimize this pattern in the compiler, there are many types like the string that don't require the isNaN." |
That just looks like a missing optimization.
That should compile to the equivalent of |
Yes, this data is different so I see slightly different results. @randall77 makes a good point about how this can be fixed. In the meantime, I'm not sure whether the doc comment should be modified. The new |
Change https://go.dev/cl/503795 mentions this issue: |
Change https://go.dev/cl/503815 mentions this issue: |
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]>
I've updated the issue title to better reflect the current understanding. @randall77 's https://go.dev/cl/503795 fixes this, but we're not sure whether this fix is worth it for inclusion in 1.21 or will have to wait for 1.22 In the meantime, my inclination is not to do anything about the documentation, given the special circumstances where it's inaccurate (only for sorted inputs, and even then, only for some sorted inputs). Happy to hear other opinions |
For golang#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]>
For golang#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]>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Use the following benchmark code
code
What did you expect to see?
Expected document to be successfully validated
Note: consider using the new slices.Sort function, which runs faster
What did you see instead?
│ sort.txt │ slices.txt │ │ sec/op │ sec/op vs base │ StringSort-4 123.7m ± 2% 134.0m ± 1% +8.36% (p=0.002 n=10)
The test results do not match the document, which says slices is faster, but the test found that sort is faster.
The text was updated successfully, but these errors were encountered: