Skip to content

Commit

Permalink
adding array PopulateCosts into base for benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
DanHarltey committed Mar 4, 2024
1 parent 334c1af commit b5cca54
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 68 deletions.
40 changes: 20 additions & 20 deletions benchmarks/Fastenshtein.Benchmarking/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,28 @@ static void Main(string[] args)
{
DateTime startTime = DateTime.UtcNow;

////if (args.Length != 0 && string.Equals(args[0], "d", StringComparison.OrdinalIgnoreCase))
////{
//// _ = BenchmarkRunner.Run<FastenshteinDisassembly>();
////}
////else if (args.Length != 0 && string.Equals(args[0], "c", StringComparison.OrdinalIgnoreCase))
////{
//// _ = BenchmarkRunner.Run<CompetitiveBenchmarkSmallWordsSingleThread>();
//// _ = BenchmarkRunner.Run<CompetitiveBenchmarkNormalWordsSingleThread>();
//// _ = BenchmarkRunner.Run<CompetitiveBenchmarkLargeWordsSingleThread>();
if (args.Length != 0 && string.Equals(args[0], "d", StringComparison.OrdinalIgnoreCase))
{
_ = BenchmarkRunner.Run<FastenshteinDisassembly>();
}
else if (args.Length != 0 && string.Equals(args[0], "c", StringComparison.OrdinalIgnoreCase))
{
_ = BenchmarkRunner.Run<CompetitiveBenchmarkSmallWordsSingleThread>();
_ = BenchmarkRunner.Run<CompetitiveBenchmarkNormalWordsSingleThread>();
_ = BenchmarkRunner.Run<CompetitiveBenchmarkLargeWordsSingleThread>();

//// _ = BenchmarkRunner.Run<CompetitiveBenchmarkSmallWordsMultiThread>();
//// _ = BenchmarkRunner.Run<CompetitiveBenchmarkNormalWordsMultiThread>();
//// _ = BenchmarkRunner.Run<CompetitiveBenchmarkLargeWordsMultiThread>();
////}
////else
////{
//// _ = BenchmarkRunner.Run<BenchmarkSmallWordsSingleThread>();
//// _ = BenchmarkRunner.Run<BenchmarkNormalWordsSingleThread>();
//// _ = BenchmarkRunner.Run<BenchmarkLargeWordsSingleThread>();
////}
_ = BenchmarkRunner.Run<CompetitiveBenchmarkSmallWordsMultiThread>();
_ = BenchmarkRunner.Run<CompetitiveBenchmarkNormalWordsMultiThread>();
_ = BenchmarkRunner.Run<CompetitiveBenchmarkLargeWordsMultiThread>();
}
else
{
_ = BenchmarkRunner.Run<BenchmarkSmallWordsSingleThread>();
_ = BenchmarkRunner.Run<BenchmarkNormalWordsSingleThread>();
_ = BenchmarkRunner.Run<BenchmarkLargeWordsSingleThread>();
}

_ = BenchmarkRunner.Run<ArrayBenchmark>();
////_ = BenchmarkRunner.Run<ArrayBenchmark>();

Console.WriteLine("Completed in : " + (DateTime.UtcNow - startTime));
}
Expand Down
41 changes: 41 additions & 0 deletions src/Fastenshtein/ArrayFill.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#if NET8_0_OR_GREATER
using System.Numerics;
#endif
using System.Runtime.CompilerServices;

namespace Fastenshtein
{
internal static class ArrayFill
{
#if NET8_0_OR_GREATER
private static readonly int[] indexes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
private static readonly Vector<int> additionVector = new(Vector<int>.Count);
private static readonly Vector<int> indexesVector = new(indexes);
#endif

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void PopulateCosts(int[] costs)
{
int i = 0;
#if NET8_0_OR_GREATER
if (Vector.IsHardwareAccelerated)
{
int lastBlockIndex = costs.Length - (costs.Length % Vector<int>.Count);

var previous = indexesVector;

while (i < lastBlockIndex)
{
previous.StoreUnsafe(ref costs[i]);
previous += additionVector;
i += Vector<int>.Count;
}
}
#endif
for (; i < costs.Length; i++)
{
costs[i] = i;
}
}
}
}
5 changes: 1 addition & 4 deletions src/Fastenshtein/AutoCompleteLevenshtein.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ public static int Distance(string value1, string value2)
int[] costs = new int[value1.Length];

// Add indexing for insertion to first row
for (int i = 0; i < costs.Length;)
{
costs[i] = ++i;
}
ArrayFill.PopulateCosts(costs);

int minSize = value1.Length < value2.Length ? value1.Length : value2.Length;

Expand Down
5 changes: 1 addition & 4 deletions src/Fastenshtein/Levenshtein.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ public int DistanceFrom(string value)
}

// Add indexing for insertion to first row
for (int i = 0; i < this.costs.Length;)
{
this.costs[i] = ++i;
}
ArrayFill.PopulateCosts(costs);

for (int i = 0; i < value.Length; i++)
{
Expand Down
5 changes: 1 addition & 4 deletions src/Fastenshtein/StaticLevenshtein.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ public static int Distance(string value1, string value2)
int[] costs = new int[value2.Length];

// Add indexing for insertion to first row
for (int i = 0; i < costs.Length;)
{
costs[i] = ++i;
}
ArrayFill.PopulateCosts(costs);

for (int i = 0; i < value1.Length; i++)
{
Expand Down
68 changes: 34 additions & 34 deletions tests/Fastenshtein.Tests/ArrayFillTests.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
namespace Fastenshtein.Tests
{
using Fastenshtein.Benchmarking;
using System.Linq;
using Xunit;
////namespace Fastenshtein.Tests
////{
//// using Fastenshtein.Benchmarking;
//// using System.Linq;
//// using Xunit;

public class ArrayFillTests
{
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(4)]
[InlineData(8)]
[InlineData(9)]
[InlineData(1024)]
public void Repeated_Distance_Calls_Return_Correct_Distances(int length)
{
var onTest = new ArrayBenchmark();
onTest.N = length;
//// public class ArrayFillTests
//// {
//// [Theory]
//// [InlineData(0)]
//// [InlineData(1)]
//// [InlineData(4)]
//// [InlineData(8)]
//// [InlineData(9)]
//// [InlineData(1024)]
//// public void Repeated_Distance_Calls_Return_Correct_Distances(int length)
//// {
//// var onTest = new ArrayBenchmark();
//// onTest.N = length;

var methods = onTest
.GetType()
.GetMethods()
.Where(x => x.ReturnType == typeof(int[]));
//// var methods = onTest
//// .GetType()
//// .GetMethods()
//// .Where(x => x.ReturnType == typeof(int[]));

foreach (var method in methods)
{
onTest.SetUp();
//// foreach (var method in methods)
//// {
//// onTest.SetUp();

var array = (int[])method.Invoke(onTest, null);
//// var array = (int[])method.Invoke(onTest, null);

for (var i = 0; i < array.Length; i++)
{
Assert.Equal(i, array[i]);
}
}
}
}
}
//// for (var i = 0; i < array.Length; i++)
//// {
//// Assert.Equal(i, array[i]);
//// }
//// }
//// }
//// }
////}
3 changes: 1 addition & 2 deletions tests/Fastenshtein.Tests/Fastenshtein.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down Expand Up @@ -31,7 +31,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\benchmarks\Fastenshtein.Benchmarking\Fastenshtein.Benchmarking.csproj" />
<ProjectReference Include="..\..\src\Fastenshtein\Fastenshtein.csproj" />
</ItemGroup>

Expand Down

0 comments on commit b5cca54

Please sign in to comment.