Files
Shogi/Benchmarking/Benchmarks.cs

114 lines
2.9 KiB
C#

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Running;
using Gameboard.ShogiUI.Rules;
using System;
using System.Linq;
using System.Numerics;
namespace Benchmarking
{
public class Benchmarks
{
private readonly Move[] moves;
private readonly Vector2[] directions;
private readonly Consumer consumer = new();
public Benchmarks()
{
moves = new[]
{
// P1 Rook
new Move { From = new Vector2(7, 1), To = new Vector2(4, 1) },
// P2 Gold
new Move { From = new Vector2(3, 8), To = new Vector2(2, 7) },
// P1 Pawn
new Move { From = new Vector2(4, 2), To = new Vector2(4, 3) },
// P2 other Gold
new Move { From = new Vector2(5, 8), To = new Vector2(6, 7) },
// P1 same Pawn
new Move { From = new Vector2(4, 3), To = new Vector2(4, 4) },
// P2 Pawn
new Move { From = new Vector2(4, 6), To = new Vector2(4, 5) },
// P1 Pawn takes P2 Pawn
new Move { From = new Vector2(4, 4), To = new Vector2(4, 5) },
// P2 King
new Move { From = new Vector2(4, 8), To = new Vector2(4, 7) },
// P1 Pawn promotes
new Move { From = new Vector2(4, 5), To = new Vector2(4, 6), IsPromotion = true },
// P2 King retreat
new Move { From = new Vector2(4, 7), To = new Vector2(4, 8) },
};
var rand = new Random();
directions = new Vector2[10];
for (var n = 0; n < 10; n++) directions[n] = new Vector2(rand.Next(-2, 2), rand.Next(-2, 2));
}
//[Benchmark]
public void One()
{
var board = new ShogiBoard();
foreach (var move in moves)
{
board.Move(move);
}
}
//[Benchmark]
public void Two()
{
var board = new ShogiBoard();
foreach (var move in moves)
{
//board.TryMove2(move);
}
}
public Vector2 FindDirection(Vector2[] directions, Vector2 destination)
{
var smallerDistance = float.MaxValue;
Vector2 found = Vector2.Zero;
foreach (var d in directions)
{
var distance = Vector2.Distance(d, destination);
if (distance < smallerDistance)
{
smallerDistance = distance;
found = d;
}
}
return found;
}
public Vector2 FindDirectionLinq(Vector2[] directions, Vector2 destination) =>
directions.Aggregate((a, b) => Vector2.Distance(destination, a) < Vector2.Distance(destination, b) ? a : b);
[Benchmark]
public void Directions_A()
{
FindDirection(directions, new Vector2(8, 7));
}
[Benchmark]
public void Directions_B()
{
FindDirectionLinq(directions, new Vector2(8, 7));
}
}
public class Program
{
public static void Main(string[] args)
{
BenchmarkRunner.Run<Benchmarks>();
Console.WriteLine("Done");
}
}
}