Python for programming chess

Discussion of anything and everything relating to chess playing software and machines.

Moderator: Ras

User avatar
Ras
Posts: 2701
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: Python for programming chess

Post by Ras »

evert823 wrote: Thu Aug 22, 2024 12:44 pm2. Will an object-oriented approach slow it down?
Not per se - it depends on how you implement it.
Suppose I have the position in a class. In order to check a move, I create a new instance/object and that happens recursively. Woeld this somehow impact performance negatively?
Unless the compiler can optimise that away, it sounds bad. Allocating the object, running its constructor (even if allocated on the stack), copying over the data, finally running the deconstructor and deallocating (heap is much worse than stack).

If, however, you hand over your object by reference (i.e. basically handing around pointers to the object) and modify / unmodify it, then you don't instantiate anything during search. Obviously, you then need to think about how to manage your move stack - instead of piling up that stack in the form of position objects, you could have that as object member. Like, current position and move stack inside the object. You'd also have a make_move() member function that takes the move to make on the board member. It would modify the board member and push this move on the stack member. Then also an move_unmake() function that pops the top move from the stack and reverts that move in the board member.
Rasmus Althoff
https://www.ct800.net
User avatar
towforce
Posts: 12514
Joined: Thu Mar 09, 2006 12:57 am
Location: Birmingham UK
Full name: Graham Laight

Re: Python for programming chess

Post by towforce »

A quick opinion:

Python is easy to write, and this explains its popularity.

When written well, C# is the most maintainable language.

Roughly speaking, the closer you go to the metal, the faster the code will run - so ultimately you should be writing your chess program in assembly language.

I think that the kids learn Python at school, and hence that becomes the language they like. In a world of LLMs, it's not so important to have a language that's easy to write, because they will give you a lot of help there.

If your program is complex, maintainability becomes important - so there is a good case for C#. C# is also fast, and it's garbage collection algorithms are good, giving you one less thing to have to think about.

I suspect you'll go for C++ (or even C) to get a little bit more speed in the end - but not go all the way to assembly language.

If I ever write a chess program, right now I'd use C#, and reference chess libraries to do the mundane tasks like move generation, so that I could focus on the fun part - the evaluation.
Human chess is partly about tactics and strategy, but mostly about memory
evert823
Posts: 38
Joined: Thu Oct 29, 2020 9:32 am
Full name: Evert Jan Karman

Re: Python for programming chess

Post by evert823 »

Ras wrote: Thu Aug 22, 2024 8:20 pm
evert823 wrote: Thu Aug 22, 2024 12:44 pm2. Will an object-oriented approach slow it down?
Not per se - it depends on how you implement it.
Suppose I have the position in a class. In order to check a move, I create a new instance/object and that happens recursively. Woeld this somehow impact performance negatively?
Unless the compiler can optimise that away, it sounds bad. Allocating the object, running its constructor (even if allocated on the stack), copying over the data, finally running the deconstructor and deallocating (heap is much worse than stack).
After eliminating some creation of objects in the performance-critical code, the performance improved a lot.

Then after that, I re-wrote it in C#, 1 to 1 translation but still manually written.
In a couple of testcases, the C# code is between 2 and 3 times faster than the python code. (*)
(*) This comparison is based on pypy as sort of python-compiler. Plain python still runs much and much slower.
User avatar
towforce
Posts: 12514
Joined: Thu Mar 09, 2006 12:57 am
Location: Birmingham UK
Full name: Graham Laight

Re: Python for programming chess

Post by towforce »

evert823 wrote: Fri Aug 30, 2024 8:31 pmAfter eliminating some creation of objects in the performance-critical code, the performance improved a lot.

Then after that, I re-wrote it in C#, 1 to 1 translation but still manually written.
In a couple of testcases, the C# code is between 2 and 3 times faster than the python code. (*)
(*) This comparison is based on pypy as sort of python-compiler. Plain python still runs much and much slower.

Thanks for sharing. Impressive speedup.
Human chess is partly about tactics and strategy, but mostly about memory
User avatar
flok
Posts: 598
Joined: Tue Jul 03, 2018 10:19 am
Full name: Folkert van Heusden

Re: Python for programming chess

Post by flok »

evert823 wrote: Fri Aug 30, 2024 8:31 pm
Ras wrote: Thu Aug 22, 2024 8:20 pm
evert823 wrote: Thu Aug 22, 2024 12:44 pm2. Will an object-oriented approach slow it down?
Not per se - it depends on how you implement it.
Suppose I have the position in a class. In order to check a move, I create a new instance/object and that happens recursively. Woeld this somehow impact performance negatively?
Unless the compiler can optimise that away, it sounds bad. Allocating the object, running its constructor (even if allocated on the stack), copying over the data, finally running the deconstructor and deallocating (heap is much worse than stack).
After eliminating some creation of objects in the performance-critical code, the performance improved a lot.

Then after that, I re-wrote it in C#, 1 to 1 translation but still manually written.
In a couple of testcases, the C# code is between 2 and 3 times faster than the python code. (*)
(*) This comparison is based on pypy as sort of python-compiler. Plain python still runs much and much slower.
Is your python code on github?
evert823
Posts: 38
Joined: Thu Oct 29, 2020 9:32 am
Full name: Evert Jan Karman

Re: Python for programming chess

Post by evert823 »

flok wrote: Sat Aug 31, 2024 9:04 am Is your python code on github?
Old C# code from years back:
https://github.com/evert823/TheWeirdEng ... Backend.cs
Python:
https://github.com/evert823/chesspython
C# re-write from Python:
https://github.com/evert823/TheWeirdEng ... eFinder.cs