Making Rust faster than C with this one simple trick (optimization for levenshtein) #346
+2
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have:
Description of changes
Replacing the inclusive ranges
1..=n
and1..=m
by exclusive ranges1..n + 1
and1..m + 1
in the Rust version of Levenshtein improves the performance by about 25% on my machine (making it about 3% faster than the C version). The reason for that is that the implementation of inclusive ranges accommodates for the fact that the upper bound might be the largest representable integer (e.g.1..=usize::MAX
) by adding additional code that is executed in every iteration of the loop, which might also prevent certain compiler optimizations from running, making the performance even worse.