Skip to content

Commit

Permalink
accelerate by 5% on simple string and 2% on complex cases
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
maruel committed Jun 3, 2022
1 parent e294315 commit e76a061
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -48,4 +48,3 @@ PASS
coverage: 100.0% of statements
ok github.com/maruel/natural 0.012s
```

6 changes: 3 additions & 3 deletions natsort.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e76a061

Please sign in to comment.