One of the fastest .Net Levenshtein projects around.
Fastenshtein is an optimized and fully unit tested Levenshtein implementation. It is optimized for speed and memory usage.
From the included brenchmarking tests comparing random words of 3 to 20 random chars to other Nuget Levenshtein implementations.
Method | Mean | Ratio | Rank | Gen0 | Allocated | Alloc Ratio |
---|---|---|---|---|---|---|
Fastenshtein | 1.077 ms | 1.00 | 1 | - | 6345 B | 1.000 |
FastenshteinStatic | 1.122 ms | 1.04 | 2 | 3.9063 | 265441 B | 41.835 |
NinjaNye | 1.899 ms | 1.76 | 4 | 76.1719 | 4274593 B | 673.695 |
StringSimilarity | 2.899 ms | 2.69 | 5 | 7.8125 | 543770 B | 85.701 |
FuzzyStringsNetStandard | 7.351 ms | 6.81 | 6 | 414.0625 | 22967283 B | 3,619.745 |
int levenshteinDistance = Fastenshtein.Levenshtein.Distance("value1", "value2");
Alternative method for comparing one item against many (quicker due to less memory allocation, not thread safe)
Fastenshtein.Levenshtein lev = new Fastenshtein.Levenshtein("value1");
foreach (var item in new []{ "value2", "value3", "value4"})
{
int levenshteinDistance = lev.DistanceFrom(item);
}
We will create Fastenshtein as a CLR Scalar-Valued Function within SQL Server. This will allow the fast Levenshtein implementationto be used within SQL Server.
sp_configure 'clr enabled', 1
RECONFIGURE
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'clr strict security', 0;
RECONFIGURE;
OR
CREATE ASSEMBLY FastenshteinAssembly FROM 'C:\Fastenshtein.dll' WITH PERMISSION_SET = SAFE
CREATE FUNCTION [Levenshtein](@value1 [nvarchar](MAX), @value2 [nvarchar](MAX))
RETURNS [int]
AS
EXTERNAL NAME [FastenshteinAssembly].[Fastenshtein.Levenshtein].[Distance]
GO
-- Usage
DECLARE @retVal as integer
select @retVal = [dbo].[Levenshtein]('Test','test')
Select @retVal