Skip to content

Commit

Permalink
perf: use CMOV instruction to speed up min()
Browse files Browse the repository at this point in the history
This increases performance of `min()` by 1-18% depending on scenario.

```
                              │   old.txt    │               new.txt               │
                              │    sec/op    │   sec/op     vs base                │
LevenshteinDistance-8            61.08n ± 0%   56.71n ± 0%   -7.15% (p=0.000 n=10)
LevenshteinDistanceBigLate-8    11.239µ ± 0%   9.230µ ± 0%  -17.88% (p=0.000 n=10)
LevenshteinDistanceBigEarly-8    9.424µ ± 0%   9.295µ ± 0%   -1.37% (p=0.000 n=10)
geomean                          1.863µ        1.694µ        -9.06%
```

Fixes #50
  • Loading branch information
lithammer committed Apr 28, 2023
1 parent 61128a1 commit a928105
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions fuzzy/levenshtein.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ func LevenshteinDistance(s, t string) int {
return column[len(r1)]
}

func min(a, b, c int) int {
if a < b && a < c {
func min2(a, b int) int {
if a < b {
return a
} else if b < c {
return b
}
return c
return b
}

func min(a, b, c int) int {
return min2(min2(a, b), c)
}

0 comments on commit a928105

Please sign in to comment.