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.
- To enable CLR integration for the server:
sp_configure 'clr enabled', 1
RECONFIGURE
- Beginning with SQL Server 2017 (14.x). Either configure CLR strict security or run the below to disable it.
EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;
EXEC sp_configure 'clr strict security', 0;
RECONFIGURE;
- Place the Fastenshtein.dll on the same computer as the SQL Server instance in a directory that the SQL Server instance has access to. You must use the .Net framework version 4.5.2 of Fastenshtein. To create the assembly (dll) either:
- Compile the project “Fastenshtein” in Release config in Visual Studio.
OR
- Download the pre-compiled dll from nuget unzip the package and use the dll in \lib\net452 folder.
- Create the assembly
CREATE ASSEMBLY FastenshteinAssembly FROM 'C:\Fastenshtein.dll' WITH PERMISSION_SET = SAFE
- Then create the function
CREATE FUNCTION [Levenshtein](@value1 [nvarchar](MAX), @value2 [nvarchar](MAX))
RETURNS [int]
AS
EXTERNAL NAME [FastenshteinAssembly].[Fastenshtein.Levenshtein].[Distance]
GO
- It is now ready to be used:
-- Usage
DECLARE @retVal as integer
select @retVal = [dbo].[Levenshtein]('Test','test')
Select @retVal