UTests pass with Pathfinder2D
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
using BenchmarkDotNet.Attributes;
|
||||
using BenchmarkDotNet.Engines;
|
||||
using BenchmarkDotNet.Running;
|
||||
using Gameboard.ShogiUI.BoardState;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Benchmarking
|
||||
@@ -9,6 +11,8 @@ namespace Benchmarking
|
||||
public class Benchmarks
|
||||
{
|
||||
private readonly Move[] moves;
|
||||
private readonly Vector2[] directions;
|
||||
private readonly Consumer consumer = new Consumer();
|
||||
|
||||
public Benchmarks()
|
||||
{
|
||||
@@ -35,9 +39,13 @@ namespace Benchmarking
|
||||
// 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]
|
||||
//[Benchmark]
|
||||
public void One()
|
||||
{
|
||||
var board = new ShogiBoard();
|
||||
@@ -47,7 +55,7 @@ namespace Benchmarking
|
||||
}
|
||||
}
|
||||
|
||||
[Benchmark]
|
||||
//[Benchmark]
|
||||
public void Two()
|
||||
{
|
||||
var board = new ShogiBoard();
|
||||
@@ -57,15 +65,49 @@ namespace Benchmarking
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
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>();
|
||||
BenchmarkRunner.Run<Benchmarks>();
|
||||
Console.WriteLine("Done");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user