Recently I started with a Python-based chess project.
Now it has correct alpha-beta (I think), and some initial 3-plies search for prioritizing candidates. It's doing fine with not too complex positions or not too many pieces, and if I run with pypy.
Unfortunately, I estimate that it will need between 24 and 48 hours for this position:
https://www.chess.com/forum/view/more-p ... e-61176239
Several years ago I had written a C# program. I was not satisfied with performance, alpha-beta was not completely implemented yet.
Code was way more messy. But at the moment, this old C# program is still 20 times faster than this new Python program.
Python for programming chess
Moderator: Ras
-
- Posts: 38
- Joined: Thu Oct 29, 2020 9:32 am
- Full name: Evert Jan Karman
-
- Posts: 12511
- Joined: Thu Mar 09, 2006 12:57 am
- Location: Birmingham UK
- Full name: Graham Laight
Re: Python for programming chess
Why do you think that your Python program is an order of magnitude slower than your C# program?
Human chess is partly about tactics and strategy, but mostly about memory
-
- Posts: 38
- Joined: Thu Oct 29, 2020 9:32 am
- Full name: Evert Jan Karman
-
- Posts: 97
- Joined: Fri Jun 28, 2024 9:24 am
- Full name: Wallace Shawn
Re: Python for programming chess
Python is not really a good choice for engine programming. It is indeed much slower than C#
-
- Posts: 596
- Joined: Tue Jul 03, 2018 10:19 am
- Full name: Folkert van Heusden
Re: Python for programming chess
If you look for a python compiler that produces code which is for speed similar to c++ (yes I tested that), take a look at shedskin: https://github.com/shedskin/shedskin/
-
- Posts: 38
- Joined: Thu Oct 29, 2020 9:32 am
- Full name: Evert Jan Karman
Re: Python for programming chess
shedskin doesn't work, giving this error message while trying to install it:flok wrote: ↑Mon Aug 19, 2024 9:56 amIf you look for a python compiler that produces code which is for speed similar to c++ (yes I tested that), take a look at shedskin: https://github.com/shedskin/shedskin/
Permission denied: 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\Lib\\site-packages\\test-easy-install-11080.write-test'
And I couldn't really find any mentioning of somebody else, who has run into this same problem while using shedskin for transpiling python.
-
- Posts: 38
- Joined: Thu Oct 29, 2020 9:32 am
- Full name: Evert Jan Karman
Re: Python for programming chess
Since my current python program is quite concise, to rewrite it manually might be as good as going for a shedskin or cython or cmake approach.
That leads to two more questions for now.
1. Can I assume that C#, C++ and C are more or less equally good choices for this - from performance point of view?
2. Will an object-oriented approach slow it down?
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?
(It seems unavoidable that we somehow construct the position after a hypothetical move, in order to check it.)
That leads to two more questions for now.
1. Can I assume that C#, C++ and C are more or less equally good choices for this - from performance point of view?
2. Will an object-oriented approach slow it down?
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?
(It seems unavoidable that we somehow construct the position after a hypothetical move, in order to check it.)
-
- Posts: 354
- Joined: Thu Jul 21, 2022 12:30 am
- Full name: Chesskobra
Re: Python for programming chess
I am not an expert programmer or language expert. But is the new object allocated and freed every time? That should be avoided. Maybe you could use a stack and go up and down the stack for make and unmake move. That is what I am doing for a game.evert823 wrote: ↑Thu Aug 22, 2024 12:44 pm
2. Will an object-oriented approach slow it down?
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?
(It seems unavoidable that we somehow construct the position after a hypothetical move, in order to check it.)
-
- Posts: 596
- Joined: Tue Jul 03, 2018 10:19 am
- Full name: Folkert van Heusden
Re: Python for programming chess
Ah windows. Try running it as administrator or sacrifice a lamb to the godsevert823 wrote: ↑Tue Aug 20, 2024 11:46 pmshedskin doesn't work, giving this error message while trying to install it:flok wrote: ↑Mon Aug 19, 2024 9:56 amIf you look for a python compiler that produces code which is for speed similar to c++ (yes I tested that), take a look at shedskin: https://github.com/shedskin/shedskin/
Permission denied: 'C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\\Lib\\site-packages\\test-easy-install-11080.write-test'
And I couldn't really find any mentioning of somebody else, who has run into this same problem while using shedskin for transpiling python.
-
- Posts: 3330
- Joined: Wed Mar 10, 2010 10:18 pm
- Location: Hamburg, Germany
- Full name: Srdja Matovic
Re: Python for programming chess
It depends on implementation, AFAIK C# has an garbage collector for memory, slows down, IMHO it was shown with CFish and AsmFish that the further you go close to the metal the more performance you gain.
https://www.chessprogramming.org/CFish
https://www.chessprogramming.org/AsmFish
--
Srdja