From e76a061d070019ecd0abcd792c7273d95e227d3c Mon Sep 17 00:00:00 2001 From: Marc-Antoine Ruel Date: Fri, 3 Jun 2022 10:36:55 -0400 Subject: [PATCH] accelerate by 5% on simple string and 2% on complex cases MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Acceleration is only visible on Intel i7 10th gen, not visible on Raspberry Pi 3 (but there's no regression either). $ go install github.com/maruel/pat/cmd/...@latest $ ba go test -bench . -benchtime 100ms -count 5 -run ^$ -cpu 1 Checking out origin/main go test -bench . -benchtime 100ms -count 5 -run ^$ -cpu 1 Checking out accel go test -bench . -benchtime 100ms -count 5 -run ^$ -cpu 1 Checking out origin/main go test -bench . -benchtime 100ms -count 5 -run ^$ -cpu 1 Checking out accel name old time/op new time/op delta LessDigitsTwoGroupsNative 3.63ns ± 0% 3.64ns ± 1% ~ (p=0.440 n=8+10) LessDigitsTwoGroups 37.9ns ± 1% 37.1ns ± 2% -2.11% (p=0.000 n=10+10) LessStringOnly 8.09ns ± 1% 7.69ns ± 0% -4.94% (p=0.000 n=10+10) LessDigitsOnly 17.8ns ± 0% 17.8ns ± 2% ~ (p=0.586 n=8+10) Less10Blocks 198ns ± 1% 194ns ± 1% -1.93% (p=0.000 n=10+10) name old alloc/op new alloc/op delta LessDigitsTwoGroupsNative 0.00B 0.00B ~ (all equal) LessDigitsTwoGroups 0.00B 0.00B ~ (all equal) LessStringOnly 0.00B 0.00B ~ (all equal) LessDigitsOnly 0.00B 0.00B ~ (all equal) Less10Blocks 0.00B 0.00B ~ (all equal) name old allocs/op new allocs/op delta LessDigitsTwoGroupsNative 0.00 0.00 ~ (all equal) LessDigitsTwoGroups 0.00 0.00 ~ (all equal) LessStringOnly 0.00 0.00 ~ (all equal) LessDigitsOnly 0.00 0.00 ~ (all equal) Less10Blocks 0.00 0.00 ~ (all equal) --- README.md | 21 ++++++++++----------- natsort.go | 6 +++--- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index e3ac346..7d1a18a 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,11 @@ goos: linux goarch: amd64 pkg: github.com/maruel/natural cpu: Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz -BenchmarkLessDigitsTwoGroupsNative 329085624 3.628 ns/op 0 B/op 0 allocs/op -BenchmarkLessDigitsTwoGroups 31792579 38.07 ns/op 0 B/op 0 allocs/op -BenchmarkLessStringOnly 147547137 8.127 ns/op 0 B/op 0 allocs/op -BenchmarkLessDigitsOnly 65866989 17.92 ns/op 0 B/op 0 allocs/op -BenchmarkLess10Blocks 5997386 198.3 ns/op 0 B/op 0 allocs/op +BenchmarkLessDigitsTwoGroupsNative 331287298 3.597 ns/op 0 B/op 0 allocs/op +BenchmarkLessDigitsTwoGroups 32479050 36.55 ns/op 0 B/op 0 allocs/op +BenchmarkLessStringOnly 157775884 7.603 ns/op 0 B/op 0 allocs/op +BenchmarkLessDigitsOnly 69210796 17.52 ns/op 0 B/op 0 allocs/op +BenchmarkLess10Blocks 6331066 190.8 ns/op 0 B/op 0 allocs/op ``` On a Raspberry Pi 3: @@ -33,11 +33,11 @@ $ go test -bench=. -cpu 1 goos: linux goarch: arm pkg: github.com/maruel/natural -BenchmarkLessDigitsTwoGroupsNative 13044535 85.86 ns/op 0 B/op 0 allocs/op -BenchmarkLessDigitsTwoGroups 1576779 751.7 ns/op 0 B/op 0 allocs/op -BenchmarkLessStringOnly 8470698 141.5 ns/op 0 B/op 0 allocs/op -BenchmarkLessDigitsOnly 3674454 326.4 ns/op 0 B/op 0 allocs/op -BenchmarkLess10Blocks 314845 3821 ns/op 0 B/op 0 allocs/op +BenchmarkLessDigitsTwoGroupsNative 14181789 86.57 ns/op 0 B/op 0 allocs/op +BenchmarkLessDigitsTwoGroups 1600195 748.9 ns/op 0 B/op 0 allocs/op +BenchmarkLessStringOnly 8286034 142.3 ns/op 0 B/op 0 allocs/op +BenchmarkLessDigitsOnly 3653055 331.4 ns/op 0 B/op 0 allocs/op +BenchmarkLess10Blocks 310687 3838 ns/op 0 B/op 0 allocs/op ``` Coverage: @@ -48,4 +48,3 @@ PASS coverage: 100.0% of statements ok github.com/maruel/natural 0.012s ``` - diff --git a/natsort.go b/natsort.go index af3e6fc..cf743a7 100644 --- a/natsort.go +++ b/natsort.go @@ -21,13 +21,13 @@ import ( // This function does no memory allocation. func Less(a, b string) bool { for { - if a == b { - return false - } if p := commonPrefix(a, b); p != 0 { a = a[p:] b = b[p:] } + if len(a) == 0 { + return len(b) != 0 + } if ia := digits(a); ia > 0 { if ib := digits(b); ib > 0 { // Both sides have digits.