Skip to content

Commit

Permalink
Updating the benchmarking old version to the latest
Browse files Browse the repository at this point in the history
  • Loading branch information
DanHarltey committed Mar 4, 2024
1 parent a3506a5 commit 9864419
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public void FastenshteinStatic()
}

[Benchmark(Baseline = true)]
public void Fastenshtein_1_0_0_5()
public void Fastenshtein_1_0_0_8()
{
Parallel.For(0, words.Length, i =>
{
var levenshtein = new global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_5(words[i]);
var levenshtein = new global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_8(words[i]);
for (int j = 0; j < words.Length; j++)
{
Expand All @@ -73,13 +73,13 @@ public void Fastenshtein_1_0_0_5()
}

[Benchmark]
public void FastenshteinStatic_1_0_0_5()
public void FastenshteinStatic_1_0_0_8()
{
Parallel.For(0, words.Length, i =>
{
for (int j = 0; j < words.Length; j++)
{
global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_5.Distance(words[i], words[j]);
global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_8.Distance(words[i], words[j]);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ public void FastenshteinStatic()
}

[Benchmark(Baseline = true)]
public void Fastenshtein_1_0_0_5()
public void Fastenshtein_1_0_0_8()
{
for (int i = 0; i < words.Length; i++)
{
var levenshtein = new global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_5(words[i]);
var levenshtein = new global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_8(words[i]);

for (int j = 0; j < words.Length; j++)
{
Expand All @@ -72,13 +72,13 @@ public void Fastenshtein_1_0_0_5()
}

[Benchmark]
public void FastenshteinStatic_1_0_0_5()
public void FastenshteinStatic_1_0_0_8()
{
for (int i = 0; i < words.Length; i++)
{
for (int j = 0; j < words.Length; j++)
{
global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_5.Distance(words[i], words[j]);
global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_8.Distance(words[i], words[j]);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/Fastenshtein.Benchmarking/FastenshteinBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ public void FastenshteinStatic()
}

[Benchmark(Baseline = true)]
public void Fastenshtein_1_0_0_5()
public void Fastenshtein_1_0_0_8()
{
for (int i = 0; i < words.Length; i++)
{
var levenshtein = new global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_5(words[i]);
var levenshtein = new global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_8(words[i]);

for (int j = 0; j < words.Length; j++)
{
Expand All @@ -58,13 +58,13 @@ public void Fastenshtein_1_0_0_5()
}

[Benchmark]
public void FastenshteinStatic_1_0_0_5()
public void FastenshteinStatic_1_0_0_8()
{
for (int i = 0; i < words.Length; i++)
{
for (int j = 0; j < words.Length; j++)
{
global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_5.Distance(words[i], words[j]);
global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_8.Distance(words[i], words[j]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public int Fastenshtein()

[Benchmark(Baseline = true)]
[SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Must be an instance method for BenchmarkDotNet")]
public int Fastenshtein_1_0_0_5()
public int Fastenshtein_1_0_0_8()
{
var levenshtein = new global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_5("test");
var levenshtein = new global::Fastenshtein.Benchmarking.FastenshteinOld.Fastenshtein_1_0_0_8("test");
return levenshtein.DistanceFrom("test");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
using System.Collections.Generic;
using System.Text;

internal class Fastenshtein_1_0_0_5
internal class Fastenshtein_1_0_0_8
{
/*
* WARRING this class is performance critical (Speed).
*/

private readonly string storedValue;
private readonly int[] costs;

/// <summary>
/// Creates a new instance with a value to test other values against
/// </summary>
/// <param Name="value">Value to compare other values to.</param>
public Fastenshtein_1_0_0_5(string value)
public Fastenshtein_1_0_0_8(string value)
{
this.storedValue = value;
// Create matrix row
Expand All @@ -23,13 +27,7 @@ public Fastenshtein_1_0_0_5(string value)
/// <summary>
/// gets the length of the stored value that is tested against
/// </summary>
public int StoredLength
{
get
{
return this.storedValue.Length;
}
}
public int StoredLength => this.storedValue.Length;

/// <summary>
/// Compares a value to the stored value.
Expand All @@ -53,42 +51,50 @@ public int DistanceFrom(string value)
{
// cost of the first index
int cost = i;
int addationCost = i;
int previousCost = i;

// cache value for inner loop to avoid index lookup and bonds checking, profiled this is quicker
char value1Char = value[i];

for (int j = 0; j < this.storedValue.Length; j++)
{
int insertionCost = cost;
int currentCost = cost;

cost = addationCost;

// assigning this here reduces the array reads we do, improvment of the old version
addationCost = this.costs[j];
// assigning this here reduces the array reads we do, improvement of the old version
cost = costs[j];

if (value1Char != this.storedValue[j])
{
if (insertionCost < cost)
if (previousCost < currentCost)
{
cost = insertionCost;
currentCost = previousCost;
}

if (addationCost < cost)
if (cost < currentCost)
{
cost = addationCost;
currentCost = cost;
}

++cost;
++currentCost;
}

this.costs[j] = cost;
/*
* Improvement on the older versions.
* Swapping the variables here results in a performance improvement for modern intel CPU’s, but I have no idea why?
*/
costs[j] = currentCost;
previousCost = currentCost;
}
}

return this.costs[this.costs.Length - 1];
}

/// <summary>
/// Compares the two values to find the minimum Levenshtein distance.
/// Thread safe.
/// </summary>
/// <returns>Difference. 0 complete match.</returns>
public static int Distance(string value1, string value2)
{
if (value2.Length == 0)
Expand All @@ -108,36 +114,39 @@ public static int Distance(string value1, string value2)
{
// cost of the first index
int cost = i;
int addationCost = i;
int previousCost = i;

// cache value for inner loop to avoid index lookup and bonds checking, profiled this is quicker
char value1Char = value1[i];

for (int j = 0; j < value2.Length; j++)
{
int insertionCost = cost;

cost = addationCost;
int currentCost = cost;

// assigning this here reduces the array reads we do, improvment of the old version
addationCost = costs[j];
// assigning this here reduces the array reads we do, improvement of the old version
cost = costs[j];

if (value1Char != value2[j])
{
if (insertionCost < cost)
if (previousCost < currentCost)
{
cost = insertionCost;
currentCost = previousCost;
}

if (addationCost < cost)
if (cost < currentCost)
{
cost = addationCost;
currentCost = cost;
}

++cost;
++currentCost;
}

costs[j] = cost;
/*
* Improvement on the older versions.
* Swapping the variables here results in a performance improvement for modern intel CPU’s, but I have no idea why?
*/
costs[j] = currentCost;
previousCost = currentCost;
}
}

Expand Down

0 comments on commit 9864419

Please sign in to comment.