ugly-one / chess

0 stars 1 forks source link

The engine is slow #2

Open ugly-one opened 3 months ago

ugly-one commented 3 months ago

Doing 4 level deep analysis of all moves takes 100 seconds on my machine - see https://github.com/ugly-one/chess/blob/e4bf94c91c30b79eb7d8a2e885a8847f826a987e/Chess.Tests/PerformanceTests.cs#L21

Perhaps it's time to optimize the engine so it's faster. Before that, doing any "serious" AI might be impossible.

p-m-rasmussen commented 3 months ago

@ugly-one I noticed one thing: When the engine makes a move, it regenerates all the moves for the piece it's moving. So the moves of the queen can potentially be generated 28 times in total for every position. I see two ways to avoid it. One is to not validate the input moves. The other is to cache the moves generated for each piece.

ugly-one commented 3 months ago

A cache is now in place: https://github.com/ugly-one/chess/pull/3 We got a nice improvement! Thanks.

I think I will keep this issue open for a while. I'm quite sure we can squeeze more out of it. The question is: when does "good" becomes "good enough"

ugly-one commented 3 months ago

Changing the representation of the board to Piece[8,8] improved performance. It went from 25 seconds to 6 seconds (depth 4).

p-m-rasmussen commented 3 months ago

@ugly-one One thing I found in my engine is that IEnumerable is the devil. It's better to pass lists or, even better, arrays since otherwise it's always calling .Next() and it's harder for the runtime to optimise.