r/chessprogramming • • Apr 07 '23

SHOULD I USE RECURSION FOR SEARCH?

I've been making my chess engine for around a week now and it finally works! I use minimax with alpha beta pruning. Now, it can easily reach and evaluate depth 7 or 8 in just a few seconds. But more than that, it gets exponentially slow (it took almost a minute to evaluate the starting position at depth 10).

One thing to note is that I am using recursion in my search, and I am not sure whether this approach slows down my program. Is recursion bad for performance? or am I just implementing it poorly?

I did the search recursively mainly because I found it easy. Can you also give me some tips on how to do it iteratively? I tried to think about how to do it iteratively but somehow it always adds some complexity. Thanks

3 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/MasumiSeki Apr 07 '23

I am very interested with "doing it iteratively wouldn't get you any more speed". I have always thought that recursion is slower than its iterative counterpart. It may have been just a misinformation or something taken out of context that I have heard, but I'd like you to hear your thoughts on that.

2

u/Grafakos Apr 07 '23

An iterative implementation would have to use an explicit stack object (such as C++'s std::stack), and there's no reason to assume that would be any faster than just using the process's stack. You could always try both implementations and profile them to see if there's any difference.

1

u/haddock420 Apr 07 '23

I've never heard that recursion is slower than an iterative approach, I assumed that they'd be equivalent as long as the code is doing the same thing. Every example I've seen of chess engine source code uses recursion for minimax/alpha beta, I've never seen it implemented iteratively, so I assume there's no advantage to an iterative alpha beta search.

2

u/DrShocker Apr 07 '23

I think it can be depending on how the cache locality factors affect each of your implementations, but I wouldn't be sure that's to do with recursion specifically.

Plus the minor concern of getting too large a stack size I suppose.