Skip to content

Commit

Permalink
Small speedup by using AllocateUninitializedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
DanHarltey committed Mar 25, 2024
1 parent 9864419 commit d625a3a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/Fastenshtein/Levenshtein.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Fastenshtein
using System;

namespace Fastenshtein
{
/// <summary>
/// Measures the difference between two strings.
Expand All @@ -21,7 +23,12 @@ public Levenshtein(string value)
{
this.storedValue = value;
// Create matrix row
#if NET5_0
this.costs = GC.AllocateUninitializedArray<int>(this.storedValue.Length);
#else
this.costs = new int[this.storedValue.Length];
#endif

}

/// <summary>
Expand Down
10 changes: 7 additions & 3 deletions src/Fastenshtein/StaticLevenshtein.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Fastenshtein
using System;

namespace Fastenshtein
{
/// <summary>
/// Measures the difference between two strings.
Expand All @@ -17,9 +19,11 @@ public static int Distance(string value1, string value2)
{
return value1.Length;
}

#if net5_0
int[] costs = GC.AllocateUninitializedArray<int>(value2.Length);
#else
int[] costs = new int[value2.Length];

#endif
// Add indexing for insertion to first row
for (int i = 0; i < costs.Length;)
{
Expand Down

0 comments on commit d625a3a

Please sign in to comment.