Skip to content

Commit

Permalink
Moves generation improved. Pgn load is now almost 4x faster!
Browse files Browse the repository at this point in the history
  • Loading branch information
Geras1mleo committed Jun 1, 2022
1 parent 4cf5800 commit 49e214c
Show file tree
Hide file tree
Showing 5 changed files with 431 additions and 113 deletions.
68 changes: 50 additions & 18 deletions ChessBenchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using BenchmarkDotNet.Attributes;

using Chess;

namespace ChessBenchmarks;
Expand Down Expand Up @@ -67,6 +66,12 @@ public void MoveUsingSan()
// |-------------------- |---------:|--------:|--------:|-----:|
// | MoveUsingMoveObject | 125.9 us | 2.32 us | 4.59 us | 1 |
// | MoveUsingSan | 187.2 us | 3.50 us | 3.28 us | 2 |
//
// 1/06/2022 => not bad
// | Method | Mean | Error | StdDev | Rank |
// |-------------------- |---------:|--------:|--------:|-----:|
// | MoveUsingMoveObject | 117.5 us | 3.30 us | 9.72 us | 1 |
// | MoveUsingSan | 152.0 us | 3.15 us | 8.98 us | 2 |
}

[RankColumn]
Expand Down Expand Up @@ -127,7 +132,18 @@ public void MovesSanTrue()
// |-------------- |---------:|---------:|---------:|-----:|
// | MovesSanFalse | 492.7 us | 16.54 us | 46.93 us | 1 |
// | MovesSanTrue | 528.3 us | 14.13 us | 39.86 us | 2 |

//
// 1/06/2022 => fuck yeah
// | Method | Mean | Error | StdDev | Rank |
// |-------------- |---------:|---------:|---------:|-----:|
// | MovesSanFalse | 321.7 us | 15.32 us | 44.68 us | 1 |
// | MovesSanTrue | 341.2 us | 7.88 us | 21.83 us | 2 |
//
// 1/06/2022 2.0 => going fast as fuck
// | Method | Mean | Error | StdDev | Median | Rank |
// |-------------- |---------:|---------:|---------:|---------:|-----:|
// | MovesSanFalse | 308.5 us | 11.98 us | 33.39 us | 298.5 us | 1 |
// | MovesSanTrue | 360.5 us | 16.82 us | 47.43 us | 359.3 us | 2 |
}

public class ChessIsValidMoveBenchmark
Expand Down Expand Up @@ -173,6 +189,11 @@ public void IsValidMove()
// | Method | Mean | Error | StdDev |
// |------------ |---------:|---------:|---------:|
// | IsValidMove | 84.19 us | 2.101 us | 6.029 us |
//
// 1/06/2022 => very good
// | Method | Mean | Error | StdDev |
// |------------ |---------:|---------:|---------:|
// | IsValidMove | 69.73 us | 1.385 us | 3.210 us |
}

[MemoryDiagnoser]
Expand Down Expand Up @@ -207,6 +228,11 @@ public void FenConvertion()
// | Method | Mean | Error | StdDev | Gen 0 | Allocated |
// |-------------- |---------:|--------:|--------:|---------:|----------:|
// | FenConvertion | 467.4 us | 8.25 us | 8.10 us | 202.1484 | 414 KB |
//
// 1/06/2022 => not a huge difference
// | Method | Mean | Error | StdDev | Gen 0 | Allocated |
// |-------------- |---------:|---------:|---------:|---------:|----------:|
// | FenConvertion | 481.1 us | 21.65 us | 63.85 us | 208.0078 | 426 KB |
}

[MemoryDiagnoser]
Expand All @@ -217,23 +243,23 @@ public void PgnConvertion()
{
ChessBoard.LoadFromPgn(
@"[Event ""Live Chess""]
[Site ""Chess.com""]
[Date ""2022.01.11""]
[Round ""?""]
[White ""Milan1905""]
[Black ""Geras1mleo""]
[Result ""1-0""]
[ECO ""C47""]
[WhiteElo ""1006""]
[BlackElo ""626""]
[TimeControl ""600""]
[EndTime ""11:58:56 PST""]
[Termination ""Milan1905 won by resignation""]
[Site ""Chess.com""]
[Date ""2022.01.11""]
[Round ""?""]
[White ""Milan1905""]
[Black ""Geras1mleo""]
[Result ""1-0""]
[ECO ""C47""]
[WhiteElo ""1006""]
[BlackElo ""626""]
[TimeControl ""600""]
[EndTime ""11:58:56 PST""]
[Termination ""Milan1905 won by resignation""]
1.e4 e5 2.Nf3 Nf6 3.Nc3 Nc6 4.Bb5 Bc5 5.Bxc6 bxc6 6.Nxe5 Bxf2+ 7.Kxf2 O-O
8.d4 d5 9.exd5 cxd5 10.Nc6 Ng4+ 11.Kg1 Qf6 12.Qf1 Qxc6 13.h3 Nf6 14.Bg5
Qb6 15.Bxf6 Qxf6 16.Qxf6 gxf6 17.Nxd5 Rb8 18.Nxf6+ Kh8 19.b3 Rb4 20.c3 Bb7
21.cxb4 1-0");
1.e4 e5 2.Nf3 Nf6 3.Nc3 Nc6 4.Bb5 Bc5 5.Bxc6 bxc6 6.Nxe5 Bxf2+ 7.Kxf2 O-O
8.d4 d5 9.exd5 cxd5 10.Nc6 Ng4+ 11.Kg1 Qf6 12.Qf1 Qxc6 13.h3 Nf6 14.Bg5
Qb6 15.Bxf6 Qxf6 16.Qxf6 gxf6 17.Nxd5 Rb8 18.Nxf6+ Kh8 19.b3 Rb4 20.c3 Bb7
21.cxb4 1-0");

ChessBoard.LoadFromPgn(
@"[Event ""Live Chess""]
Expand Down Expand Up @@ -283,4 +309,10 @@ axb4 46.axb4 Rg4 47.Rh8+ Rh4 48.Rxh4+ Kxh4 49.b5 f4 50.b6 f3 51.b7 f2 52.
// | Method | Mean | Error | StdDev | Gen 0 | Allocated |
// |-------------- |---------:|----------:|----------:|----------:|----------:|
// | PgnConvertion | 7.918 ms | 0.2930 ms | 0.8641 ms | 2562.5000 | 5 MB |
//
// 1/06/2022 => HOLLY SHIT THAT ONE IS GOOOOD
// | Method | Mean | Error | StdDev | Gen 0 | Allocated |
// |-------------- |---------:|----------:|----------:|---------:|----------:|
// | PgnConvertion | 2.176 ms | 0.1057 ms | 0.3101 ms | 761.7188 | 2 MB |

}
8 changes: 4 additions & 4 deletions ChessBenchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using BenchmarkDotNet.Running;
using ChessBenchmarks;

//BenchmarkRunner.Run<ChessMoveBenchmark>();
BenchmarkRunner.Run<ChessMoveBenchmark>();
BenchmarkRunner.Run<ChessGenerateMovesBenchmark>();
//BenchmarkRunner.Run<ChessIsValidMoveBenchmark>();
BenchmarkRunner.Run<ChessIsValidMoveBenchmark>();

//BenchmarkRunner.Run<ChessFenConversionsBenchmark>();
//BenchmarkRunner.Run<ChessPgnConversionsBenchmark>();
BenchmarkRunner.Run<ChessFenConversionsBenchmark>();
BenchmarkRunner.Run<ChessPgnConversionsBenchmark>();

Console.ReadLine();
2 changes: 1 addition & 1 deletion ChessLibrary/Builders/SanBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private static Move[] GetMovesOfPieceOnPosition(Piece piece, Position newPositio
for (short i = 0; i < 8; i++)
{
for (short j = 0; j < 8; j++)
{ // todo patterns
{
if (board.pieces[i, j] is not null
&& board.pieces[i, j].Color == piece.Color
&& board.pieces[i, j].Type == piece.Type)
Expand Down
Loading

0 comments on commit 49e214c

Please sign in to comment.