Skip to content

sort: slices.Sort is slightly slower than sort.Strings on some sorted inputs #60777

Closed
@qiulaidongfeng

Description

@qiulaidongfeng

What version of Go are you using (go version)?

$ go version devel go1.21-39effbc105 Fri Jun 9 04:07:29 2023 +0000 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GOOS=windows
set GOARCH=amd64
set GOAMD64=v2

What did you do?

Use the following benchmark code

code
package main

import (
//"slices"
"sort"
"strconv"
"testing"
)

var s []string

func init() {
const n = 10000000
s = make([]string, n)
for i := 0; i < n; i++ {
s[i] = strconv.Itoa(i)
}
}

func BenchmarkStringSort(b *testing.B) {
for i := 0; i < b.N; i++ {
sort.Strings(s)
}
}

// func BenchmarkStringSort(b *testing.B) {
// for i := 0; i < b.N; i++ {
// slices.Sort(s)
// }
// }

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.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions