Skip to content

Commit

Permalink
use span to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredcnance committed Jan 8, 2018
1 parent c91eb96 commit e01fd32
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
1 change: 1 addition & 0 deletions API/API.csproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="StackifyLib" Version="2.1.0" /> <PackageReference Include="StackifyLib" Version="2.1.0" />
<PackageReference Include="StackifyMiddleware" Version="2.0.3" /> <PackageReference Include="StackifyMiddleware" Version="2.0.3" />
<PackageReference Include="System.Memory" Version="4.4.0-*" />
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>
Expand Down
41 changes: 41 additions & 0 deletions API/Services/LevenshteinFuzzySearch.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,6 +38,47 @@ public int GetLevenshteinDistance(string str1, string str2)
for (var i = 0; i <= n; d[i, 0] = i++) ; for (var i = 0; i <= n; d[i, 0] = i++) ;
for (var j = 0; j <= m; d[0, j] = j++) ; for (var j = 0; j <= m; d[0, j] = j++) ;


// Step 3
for (var i = 1; i <= n; i++)
{
//Step 4
for (var j = 1; j <= m; j++)
{
// Step 5
var a = str2.AsSpan().Slice(j - 1, 1)[0];
var b = str1.AsSpan().Slice(i - 1, 1)[0];

var cost = (a == b ? 0 : 1);

// Step 6
d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1),
d[i - 1, j - 1] + cost);
}
}

// Step 7
return d[n, m];
}

public int ComputeSimilarity_Baseline(string str1, string str2)
{
str1 = str1.ToLowerInvariant();
str2 = str2.ToLowerInvariant();

var n = str1.Length;

var m = str2.Length;

var d = new int[n + 1, m + 1];

// Step 1
if (n == 0) return m;
if (m == 0) return n;

// Step 2
for (var i = 0; i <= n; d[i, 0] = i++) ;
for (var j = 0; j <= m; d[0, j] = j++) ;

// Step 3 // Step 3
for (var i = 1; i <= n; i++) for (var i = 1; i <= n; i++)
{ {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ Frequency=2533207 Hz, Resolution=394.7565 ns, Timer=TSC
[Host] : .NET Core 2.0.3 (Framework 4.6.25815.02), 64bit RyuJIT [Host] : .NET Core 2.0.3 (Framework 4.6.25815.02), 64bit RyuJIT
DefaultJob : .NET Core 2.0.3 (Framework 4.6.25815.02), 64bit RyuJIT DefaultJob : .NET Core 2.0.3 (Framework 4.6.25815.02), 64bit RyuJIT


```



| Method | String1 | String2 | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated | ```
|-------------------- |-------------------- |------------------ |-------------:|------------:|------------:|-----------:|---------:|---------:|-----------:| | Method | String1 | String2 | Mean | Error | StdDev | Scaled | Gen 0 | Gen 1 | Gen 2 | Allocated |
| **LevenshteinDistance** | **RandomString[10000]** | **RandomString[100]** | **35,775.90 us** | **601.6207 us** | **533.3210 us** | **21062.5000** | **875.0000** | **812.5000** | **66465.6 KB** | |---------------------------- |-------------------- |------------------ |--------------:|------------:|------------:|-------:|-----------:|---------:|---------:|------------:|
| **LevenshteinDistance** | **RandomString[1000]** | **RandomString[100]** | **3,469.71 us** | **34.6071 us** | **28.8985 us** | **2035.1563** | **128.9063** | **125.0000** | **6647.18 KB** | | **LevenshteinDistance** | **RandomString[10000]** | **RandomString[100]** | **8,261.118 us** | **83.8376 us** | **74.3198 us** | **0.23** | **992.1875** | **992.1875** | **992.1875** | **3965.54 KB** |
| **LevenshteinDistance** | **RandomString[100]** | **RandomString[100]** | **362.57 us** | **4.7108 us** | **4.1760 us** | **216.3086** | **-** | **-** | **665.34 KB** | | LevenshteinDistanceBaseline | RandomString[10000] | RandomString[100] | 35,506.764 us | 227.1435 us | 212.4702 us | 1.00 | 21062.5000 | 875.0000 | 812.5000 | 66465.62 KB |
| **LevenshteinDistance** | **RandomString[10]** | **RandomString[100]** | **34.65 us** | **0.2365 us** | **0.2097 us** | **21.8506** | **-** | **-** | **67.16 KB** | | **LevenshteinDistance** | **RandomString[1000]** | **RandomString[100]** | **866.979 us** | **7.5294 us** | **6.6746 us** | **0.24** | **124.0234** | **124.0234** | **124.0234** | **397.18 KB** |
| LevenshteinDistanceBaseline | RandomString[1000] | RandomString[100] | 3,585.358 us | 45.1899 us | 42.2706 us | 1.00 | 2035.1563 | 128.9063 | 125.0000 | 6647.18 KB |
| **LevenshteinDistance** | **RandomString[100]** | **RandomString[100]** | **89.174 us** | **1.0989 us** | **1.0279 us** | **0.24** | **13.0615** | **-** | **-** | **40.34 KB** |
| LevenshteinDistanceBaseline | RandomString[100] | RandomString[100] | 364.125 us | 4.8533 us | 4.5398 us | 1.00 | 216.3086 | - | - | 665.34 KB |
| **LevenshteinDistance** | **RandomString[10]** | **RandomString[100]** | **7.996 us** | **0.0664 us** | **0.0555 us** | **0.23** | **1.5106** | **-** | **-** | **4.66 KB** |
| LevenshteinDistanceBaseline | RandomString[10] | RandomString[100] | 35.245 us | 0.3588 us | 0.3356 us | 1.00 | 21.8506 | - | - | 67.16 KB |
3 changes: 3 additions & 0 deletions Benchmarks/Services/FuzzySearch_Benchmarks.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public static IEnumerable<IParam> String2Values()


[Benchmark] [Benchmark]
public int LevenshteinDistance() => _levenshteinFuzzySearch.ComputeSimilarity(String1.Value, String2.Value); public int LevenshteinDistance() => _levenshteinFuzzySearch.ComputeSimilarity(String1.Value, String2.Value);

[Benchmark(Baseline = true)]
public int LevenshteinDistanceBaseline() => _levenshteinFuzzySearch.ComputeSimilarity_Baseline(String1.Value, String2.Value);
} }


/// <remarks> /// <remarks>
Expand Down

0 comments on commit e01fd32

Please sign in to comment.