-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pre-calculate SquareBits and place them in a static array
- Loading branch information
1 parent
828139f
commit 25e9813
Showing
6 changed files
with
87 additions
and
19 deletions.
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,71 @@ | ||
/* | ||
* | ||
* | ||
*/ | ||
|
||
using BenchmarkDotNet.Attributes; | ||
using Lynx.Model; | ||
|
||
namespace Lynx.Benchmark; | ||
|
||
file static class BitBoardHelpers | ||
{ | ||
public static readonly ulong[] SquareBitsArray = | ||
Enum.GetValuesAsUnderlyingType<BoardSquare>() | ||
.OfType<int>() | ||
.Select(square => 1UL << square) | ||
.ToArray(); | ||
|
||
public static ulong CaculateSquareBit(int boardSquare) | ||
{ | ||
return 1UL << boardSquare; | ||
} | ||
} | ||
|
||
public class SquareBit_Benchmark : BaseBenchmark | ||
{ | ||
[Params(1, 10, 100, 1_000, 10_000)] | ||
public int Size { get; set; } | ||
|
||
private BoardSquare[] _squares = null!; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
_squares = Enumerable.Range((int)BoardSquare.a1, (int)BoardSquare.h8). | ||
Select(_ => (BoardSquare)Random.Shared.Next((int)BoardSquare.a1, (int)BoardSquare.h8)) | ||
.ToArray(); | ||
} | ||
|
||
[Benchmark(Baseline = true)] | ||
public ulong Calculated() | ||
{ | ||
ulong count = 0; | ||
|
||
for (int i = 0; i < Size; ++i) | ||
{ | ||
foreach (var square in _squares) | ||
{ | ||
count += BitBoardHelpers.CaculateSquareBit((int)square); | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
|
||
[Benchmark] | ||
public ulong Array() | ||
{ | ||
ulong count = 0; | ||
|
||
for (int i = 0; i < Size; ++i) | ||
{ | ||
foreach (var square in _squares) | ||
{ | ||
count += BitBoardHelpers.SquareBitsArray[(int)square]; | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
} |
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
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