Python for programming chess

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

Moderator: Ras

evert823
Posts: 38
Joined: Thu Oct 29, 2020 9:32 am
Full name: Evert Jan Karman

Python for programming chess

Post by evert823 »

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.
User avatar
towforce
Posts: 12511
Joined: Thu Mar 09, 2006 12:57 am
Location: Birmingham UK
Full name: Graham Laight

Re: Python for programming chess

Post by towforce »

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
evert823
Posts: 38
Joined: Thu Oct 29, 2020 9:32 am
Full name: Evert Jan Karman

Re: Python for programming chess

Post by evert823 »

towforce wrote: Mon Aug 19, 2024 12:08 am Why do you think that your Python program is an order of magnitude slower than your C# program?
I assume that it is because C# is compiled and python is not. pypy does some form of compiling but that's apparently not as good as the C# compiler.
shawn
Posts: 97
Joined: Fri Jun 28, 2024 9:24 am
Full name: Wallace Shawn

Re: Python for programming chess

Post by shawn »

Python is not really a good choice for engine programming. It is indeed much slower than C#
User avatar
flok
Posts: 596
Joined: Tue Jul 03, 2018 10:19 am
Full name: Folkert van Heusden

Re: Python for programming chess

Post by flok »

evert823 wrote: Mon Aug 19, 2024 12:19 am
towforce wrote: Mon Aug 19, 2024 12:08 am Why do you think that your Python program is an order of magnitude slower than your C# program?
I assume that it is because C# is compiled and python is not. pypy does some form of compiling but that's apparently not as good as the C# compiler.
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/
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: Mon Aug 19, 2024 9:56 am
evert823 wrote: Mon Aug 19, 2024 12:19 am
towforce wrote: Mon Aug 19, 2024 12:08 am Why do you think that your Python program is an order of magnitude slower than your C# program?
I assume that it is because C# is compiled and python is not. pypy does some form of compiling but that's apparently not as good as the C# compiler.
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/
shedskin doesn't work, giving this error message while trying to install it:
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.
evert823
Posts: 38
Joined: Thu Oct 29, 2020 9:32 am
Full name: Evert Jan Karman

Re: Python for programming chess

Post by evert823 »

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.)
chesskobra
Posts: 354
Joined: Thu Jul 21, 2022 12:30 am
Full name: Chesskobra

Re: Python for programming chess

Post by chesskobra »

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.)
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.
User avatar
flok
Posts: 596
Joined: Tue Jul 03, 2018 10:19 am
Full name: Folkert van Heusden

Re: Python for programming chess

Post by flok »

evert823 wrote: Tue Aug 20, 2024 11:46 pm
flok wrote: Mon Aug 19, 2024 9:56 am
evert823 wrote: Mon Aug 19, 2024 12:19 am
towforce wrote: Mon Aug 19, 2024 12:08 am Why do you think that your Python program is an order of magnitude slower than your C# program?
I assume that it is because C# is compiled and python is not. pypy does some form of compiling but that's apparently not as good as the C# compiler.
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/
shedskin doesn't work, giving this error message while trying to install it:
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.
Ah windows. Try running it as administrator or sacrifice a lamb to the gods 😁👍
smatovic
Posts: 3330
Joined: Wed Mar 10, 2010 10:18 pm
Location: Hamburg, Germany
Full name: Srdja Matovic

Re: Python for programming chess

Post by smatovic »

evert823 wrote: Thu Aug 22, 2024 12:44 pm [..]
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?
[...]
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