]> Cypherpunks repositories - gostls13.git/commitdiff
os: use strings.EqualFold in os_test.go
authorcui fliter <imcusg@gmail.com>
Sun, 8 Oct 2023 02:51:45 +0000 (10:51 +0800)
committerGopher Robot <gobot@golang.org>
Mon, 9 Oct 2023 22:06:42 +0000 (22:06 +0000)
strings.EqualFold has no memory overhead and has better performance than strings.ToLower.

This is a performance test:

package bench

import (
"strings"
"testing"
)

func BenchmarkToLower(b *testing.B) {

str1 := "Windows"
str2 := "windows"

for i := 0; i < b.N; i++ {
if strings.ToLower(str1) == strings.ToLower(str2) {
}
}
}

func BenchmarkEqualFold(b *testing.B) {

str1 := "Windows"
str2 := "windows"

for i := 0; i < b.N; i++ {
if strings.EqualFold(str1, str2) {
}
}
}

The result:

goos: darwin
goarch: arm64
BenchmarkToLower-8      31404808                36.99 ns/op            8 B/op          1 allocs/op
BenchmarkEqualFold-8    194780793                5.989 ns/op           0 B/op          0 allocs/op
PASS

Change-Id: Id3d92534942d3eb0bdc1d01359324030ad0e434f
Reviewed-on: https://go-review.googlesource.com/c/go/+/533635
Run-TryBot: shuang cui <imcusg@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/os/os_test.go

index da70c398e82cd4d40932b56b16f0d320ec7cf183..ae12b9ce1be036e947502bb6e3b6bb654f88b332 100644 (file)
@@ -147,7 +147,7 @@ func size(name string, t *testing.T) int64 {
 func equal(name1, name2 string) (r bool) {
        switch runtime.GOOS {
        case "windows":
-               r = strings.ToLower(name1) == strings.ToLower(name2)
+               r = strings.EqualFold(name1, name2)
        default:
                r = name1 == name2
        }