Question About CPP-C#, Performance, and Square Represenation

Discussion of chess software programming and technical issues.

Moderators: bob, hgm, Harvey Williamson

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
bpfliegel
Posts: 71
Joined: Fri Mar 16, 2012 9:16 am

Re: Question About CPP-C#, Performance, and Square Represena

Post by bpfliegel » Sun Sep 30, 2012 5:58 am

"It's also important to remember that speed is not everything. If you took the #1 top engine in the world and rewrote it in C# or Java, it would still be one of the top engines."

See the proof here:
https://github.com/bpfliegel/Portfish

For me what seems to be the most common problem when implementing a chess engine is C# is memory management. Every second we could create 10+ million objects, which puts a horrible pressure on the GC, so always reuse your objects, never recreate them (see the ObjectBroker.cs for a possible solution).

C vs C# speed ratio was at the end 1:2,7 in case of Portfish.

Good luck!
Balint

rreagan
Posts: 102
Joined: Sun Sep 09, 2007 4:32 am

Re: Question About CPP-C#, Performance, and Square Represena

Post by rreagan » Sun Sep 30, 2012 4:17 pm

bpfliegel wrote:"It's also important to remember that speed is not everything. If you took the #1 top engine in the world and rewrote it in C# or Java, it would still be one of the top engines."

See the proof here:
https://github.com/bpfliegel/Portfish

For me what seems to be the most common problem when implementing a chess engine is C# is memory management. Every second we could create 10+ million objects, which puts a horrible pressure on the GC, so always reuse your objects, never recreate them (see the ObjectBroker.cs for a possible solution).

C vs C# speed ratio was at the end 1:2,7 in case of Portfish.

Good luck!
Balint
Hi Balint, that is very interesting! Do you know what the ELO difference is between Stockfish and Portfish? If Stockfish is about 2.7 times faster, I would guess the ELO difference is around 200 or so?

bpfliegel
Posts: 71
Joined: Fri Mar 16, 2012 9:16 am

Re: Question About CPP-C#, Performance, and Square Represena

Post by bpfliegel » Sun Sep 30, 2012 5:43 pm

rreagan wrote:
bpfliegel wrote:"It's also important to remember that speed is not everything. If you took the #1 top engine in the world and rewrote it in C# or Java, it would still be one of the top engines."

See the proof here:
https://github.com/bpfliegel/Portfish

For me what seems to be the most common problem when implementing a chess engine is C# is memory management. Every second we could create 10+ million objects, which puts a horrible pressure on the GC, so always reuse your objects, never recreate them (see the ObjectBroker.cs for a possible solution).

C vs C# speed ratio was at the end 1:2,7 in case of Portfish.

Good luck!
Balint
Hi Balint, that is very interesting! Do you know what the ELO difference is between Stockfish and Portfish? If Stockfish is about 2.7 times faster, I would guess the ELO difference is around 200 or so?
I think it is less (around 80 ELO), as it is stronger as Junior and Spike - well, according to my tests. So far, no real professional testing was carried out - I hope I start flaming and someone will test it for good :)

Cheers, Balint

bpfliegel
Posts: 71
Joined: Fri Mar 16, 2012 9:16 am

Re: Question About CPP-C#, Performance, and Square Represena

Post by bpfliegel » Sun Sep 30, 2012 5:44 pm

emadsen wrote:You can check out the source code to my engine for comparison. Link in my signature. Roughly equal in strength to TSCP. I have unpublished changes that add another 100 elo. Mostly due to an incremental move generator (hash move, captures of queens, then captures of rooks, etc... then non captures) which saves time if a capture causes a beta cutoff. I implemented the incremental move generator using C# enumerators and the yield statement. Very clean. Will post code in a few days.
Erik, doesn't yield kill your performance? I also experimented with it (sounds logical), but had disappointing results...

bpfliegel
Posts: 71
Joined: Fri Mar 16, 2012 9:16 am

Re: Question About CPP-C#, Performance, and Square Represena

Post by bpfliegel » Sun Sep 30, 2012 9:03 pm

bpfliegel wrote:
rreagan wrote:
bpfliegel wrote:"It's also important to remember that speed is not everything. If you took the #1 top engine in the world and rewrote it in C# or Java, it would still be one of the top engines."

See the proof here:
https://github.com/bpfliegel/Portfish

For me what seems to be the most common problem when implementing a chess engine is C# is memory management. Every second we could create 10+ million objects, which puts a horrible pressure on the GC, so always reuse your objects, never recreate them (see the ObjectBroker.cs for a possible solution).

C vs C# speed ratio was at the end 1:2,7 in case of Portfish.

Good luck!
Balint
Hi Balint, that is very interesting! Do you know what the ELO difference is between Stockfish and Portfish? If Stockfish is about 2.7 times faster, I would guess the ELO difference is around 200 or so?
I think it is less (around 80 ELO), as it is stronger as Junior and Spike - well, according to my tests. So far, no real professional testing was carried out - I hope I start flaming and someone will test it for good :)

Cheers, Balint
Sorry, wanted to say -120 ELO (was absent in school, when they taught substraction). It is still less than expected by myself.

Best, Balint

User avatar
emadsen
Posts: 201
Joined: Wed Apr 25, 2012 11:51 pm
Location: Oak Park, IL, USA
Full name: Erik Madsen
Contact:

Re: Question About CPP-C#, Performance, and Square Represena

Post by emadsen » Mon Oct 01, 2012 5:39 pm

Erik, doesn't yield kill your performance? I also experimented with it (sounds logical), but had disappointing results.
I don't know. I haven't tested it extensively. The C# yield statement frees me from having to track state inside an enumerator. So I get the benefit of staged move generation (faster compared to generating all moves) without complicating the code. Considering my goal is to write a bug-free chess program from scratch, of modest playing strength- this is good enough for me.

I'd imagine your task is quite different. You're converting a world class codebase to another programming language. I get to look for big performance wins. You inherited hundreds of performance wins. Your job is to avoid performance losses :)
My C# chess engine: https://www.madchess.net

bpfliegel
Posts: 71
Joined: Fri Mar 16, 2012 9:16 am

Re: Question About CPP-C#, Performance, and Square Represena

Post by bpfliegel » Mon Oct 01, 2012 6:42 pm

emadsen wrote:You inherited hundreds of performance wins. Your job is to avoid performance losses :)
You put it into a quite interesting perspective. But it is quite correct I have to say - and was not an easy task at all :)

User avatar
emadsen
Posts: 201
Joined: Wed Apr 25, 2012 11:51 pm
Location: Oak Park, IL, USA
Full name: Erik Madsen
Contact:

Re: Question About CPP-C#, Performance, and Square Represena

Post by emadsen » Tue Oct 02, 2012 1:18 am

Balint,

No doubt it was a lot of work. Every language / framework has its own pitfalls, performance traps, and counter-intuitive behaviors.

The performance you get with C# is quite impressive, though not surprising to me. It is often the case that people tout an issue of importance on the margin and promote it as if it's the essential thing. Usually this is due to the triumph of marketing (a novice rider buying an expensive bicycle) but sometimes it's done to avoid self criticism (it can't be my code, it must be the language).

I have no doubt that among high-level languages, C and C++ compilers produce the fastest machine code. But there's more to writing fast code than selecting a programming language. The quality and efficiency of data structures / algorithms have a much greater impact on performance than choice of programming language.

-Erik
My C# chess engine: https://www.madchess.net

User avatar
stegemma
Posts: 859
Joined: Mon Aug 10, 2009 8:05 pm
Location: Italy
Full name: Stefano Gemma
Contact:

Re: Question About CPP-C#, Performance, and Square Represena

Post by stegemma » Tue Oct 02, 2012 8:57 am

I suggest you to create static array of the objects that you use frequently and then just assign values to free ones, instead of creating new objects. I use two array: one for nodes and one for moves. When i store a move, i write the data in a move object then increment the node index (or the node pointer) to access the next move. The same i do for nodes. For any node, keep the index to the first move object and maybe to the last (but this one is equal to the first of the following node). Because move and node are the more created/deleted objects, this could give you a big speed-up. I use this way to do since my assembly programs and is good even for new C++ engine.

Rein Halbersma
Posts: 688
Joined: Tue May 22, 2007 9:13 am

Re: Question About CPP-C#, Performance, and Square Represena

Post by Rein Halbersma » Tue Oct 02, 2012 11:04 am

emadsen wrote:The C# yield statement frees me from having to track state inside an enumerator. So I get the benefit of staged move generation (faster compared to generating all moves) without complicating the code. Considering my goal is to write a bug-free chess program from scratch, of modest playing strength- this is good enough for me.
Hi Erik,

IIRC, in on of your first posts you mentioned that your source code can be downloaded from your website, but I can't seem to locate it. Even though I'm programming in C++, I'm interested in how the yield method would simplify code for move generation. If you could provide a link or code snippet, that would be great!

Rein

Post Reply