forked from julianperrott/WowClassicGrindBot
-
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.gitignore BenchmarkDotNet artifacts
Added new project Benchmarks Benchmarks: Added two bench
- Loading branch information
Showing
6 changed files
with
235 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="BenchmarkDotNet" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Core\Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Benchmarks.PPather; | ||
|
||
[MemoryDiagnoser] | ||
public class PPatherHasHoles | ||
{ | ||
|
||
public bool hasholes = true; | ||
|
||
public readonly uint holes; | ||
|
||
// 0 ..3, 0 ..3 | ||
private static readonly int[] old_holetab_h = [0x1111, 0x2222, 0x4444, 0x8888]; | ||
private static readonly int[] old_holetab_v = [0x000F, 0x00F0, 0x0F00, 0xF000]; | ||
|
||
private static readonly int[] new_holetab = [ | ||
0x1111 & 0x000F, 0x1111 & 0x00F0, 0x1111 & 0x0F00, 0x1111 & 0xF000, | ||
0x2222 & 0x000F, 0x2222 & 0x00F0, 0x2222 & 0x0F00, 0x2222 & 0xF000, | ||
0x4444 & 0x000F, 0x4444 & 0x00F0, 0x4444 & 0x0F00, 0x4444 & 0xF000, | ||
0x8888 & 0x000F, 0x8888 & 0x00F0, 0x8888 & 0x0F00, 0x8888 & 0xF000 | ||
]; | ||
|
||
[Benchmark(Baseline = true)] | ||
[ArgumentsSource(nameof(Inputs_Fast))] | ||
public bool IsHole_Old(int i, int j) | ||
{ | ||
if (!hasholes) | ||
return false; | ||
|
||
i /= 2; | ||
j /= 2; | ||
|
||
return i <= 3 && j <= 3 && (holes & old_holetab_h[i] & old_holetab_v[j]) != 0; | ||
} | ||
|
||
[Benchmark] | ||
[ArgumentsSource(nameof(Inputs_Fast))] | ||
public bool IsHole_New(int i, int j) | ||
{ | ||
if (!hasholes) | ||
return false; | ||
|
||
i >>= 1; | ||
j >>= 1; | ||
|
||
if (i > 3 || j > 3) | ||
return false; | ||
|
||
int index = (i << 2) | j; | ||
|
||
return (holes & new_holetab[index]) != 0; | ||
} | ||
|
||
public static IEnumerable<object[]> Inputs_All() | ||
{ | ||
const int min = -1; // -1 | ||
const int max = 5; // 5 | ||
|
||
// Generate a range of inputs from -1,-1 to 5,5 | ||
for (int i = min; i < max; i++) | ||
{ | ||
for (int j = min; j < max; j++) | ||
{ | ||
yield return [i, j]; | ||
} | ||
} | ||
} | ||
|
||
public static IEnumerable<object[]> Inputs_Fast() | ||
{ | ||
yield return [0, 0]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
using BenchmarkDotNet.Running; | ||
|
||
using Requirement; | ||
|
||
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); |
120 changes: 120 additions & 0 deletions
120
Benchmarks/Requirement/CoreRequirementCreateTargetCastingSpell.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace Requirement; | ||
|
||
internal static class PlayerReader | ||
{ | ||
public static bool IsTargetCasting() => true; | ||
|
||
public static int SpellBeingCastByTarget { get; set; } | ||
} | ||
|
||
[MemoryDiagnoser] | ||
public class CoreRequirementCreateTargetCastingSpell | ||
{ | ||
private const char SEP1 = ':'; | ||
private const char SEP2 = ','; | ||
|
||
public CoreRequirementCreateTargetCastingSpell() | ||
{ | ||
/* | ||
foreach(var input in CreateTargetCastingSpell_Inputs()) | ||
{ | ||
CreateTargetCastingSpell_Old(input); | ||
CreateTargetCastingSpell_New(input); | ||
} | ||
*/ | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
[ArgumentsSource(nameof(CreateTargetCastingSpell_Inputs))] | ||
public void Run_Old(string text) => CreateTargetCastingSpell_Old(text); | ||
|
||
[Benchmark] | ||
[ArgumentsSource(nameof(CreateTargetCastingSpell_Inputs))] | ||
public void Run_New(string text) => CreateTargetCastingSpell_New(text); | ||
|
||
public Core.Requirement CreateTargetCastingSpell_Old(string requirement) | ||
{ | ||
return create(requirement); | ||
static Core.Requirement create(string requirement) | ||
{ | ||
ReadOnlySpan<char> span = requirement; | ||
int sep1 = span.IndexOf(SEP1); | ||
// 'TargetCastingSpell' | ||
if (sep1 == -1) | ||
{ | ||
return new Core.Requirement | ||
{ | ||
HasRequirement = PlayerReader.IsTargetCasting, | ||
LogMessage = () => "Target casting" | ||
}; | ||
} | ||
|
||
// 'TargetCastingSpell:_1_?,_n_' | ||
string[] spellsPart = span[(sep1 + 1)..].ToString().Split(SEP2); | ||
HashSet<int> spellIds = spellsPart.Select(int.Parse).ToHashSet(); | ||
|
||
bool f() => spellIds.Contains(PlayerReader.SpellBeingCastByTarget); | ||
string s() => $"Target casts {PlayerReader.SpellBeingCastByTarget} ∈ [{string.Join(SEP2, spellIds)}]"; | ||
return new Core.Requirement | ||
{ | ||
HasRequirement = f, | ||
LogMessage = s | ||
}; | ||
} | ||
} | ||
|
||
[SkipLocalsInit] | ||
public Core.Requirement CreateTargetCastingSpell_New(string requirement) | ||
{ | ||
return create(requirement); | ||
static Core.Requirement create(string requirement) | ||
{ | ||
ReadOnlySpan<char> span = requirement; | ||
int sep1 = span.IndexOf(SEP1); | ||
// 'TargetCastingSpell' | ||
if (sep1 == -1) | ||
{ | ||
return new Core.Requirement | ||
{ | ||
HasRequirement = PlayerReader.IsTargetCasting, | ||
LogMessage = () => "Target casting" | ||
}; | ||
} | ||
|
||
// 'TargetCastingSpell:_1_?,_n_' | ||
Span<Range> ranges = stackalloc Range[span.Length]; | ||
ReadOnlySpan<char> values = span[(sep1 + 1)..]; | ||
int count = values.Split(ranges, SEP2); | ||
|
||
HashSet<int> spellIds = new(count); | ||
foreach (var range in ranges[..count]) | ||
{ | ||
spellIds.Add(int.Parse(values[range])); | ||
} | ||
|
||
bool f() => spellIds.Contains(PlayerReader.SpellBeingCastByTarget); | ||
string s() => $"Target casts {PlayerReader.SpellBeingCastByTarget} ∈ [{string.Join(SEP2, spellIds)}]"; | ||
return new Core.Requirement | ||
{ | ||
HasRequirement = f, | ||
LogMessage = s | ||
}; | ||
} | ||
} | ||
|
||
public static IEnumerable<string> CreateTargetCastingSpell_Inputs() | ||
{ | ||
yield return "TargetCastingSpell"; | ||
yield return "TargetCastingSpell:1"; | ||
yield return "TargetCastingSpell:1,12"; | ||
yield return "TargetCastingSpell:1,12,123"; | ||
yield return "TargetCastingSpell:1,12,123,1234"; | ||
yield return "TargetCastingSpell:4321,321,21,1"; | ||
yield return "TargetCastingSpell:4321648,3721,24841,148484"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters