C# vs C++

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

Moderators: Harvey Williamson, bob, hgm

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Post Reply
User avatar
Dan Honeycutt
Posts: 5258
Joined: Mon Feb 27, 2006 3:31 pm
Location: Atlanta, Georgia

C# vs C++

Post by Dan Honeycutt » Wed Nov 09, 2016 5:32 pm

What's the approximate speed difference (if any) for a chess program written in C# and C++?

Thanks
Dan H.

CheckersGuy
Posts: 273
Joined: Wed Aug 24, 2016 7:49 pm

Re: C# vs C++

Post by CheckersGuy » Wed Nov 09, 2016 7:34 pm

I dont want to open another thread for asking a smiliar question How much worse (speed/elo) is a chess engine that's written entirely in java ?
Last edited by CheckersGuy on Wed Nov 09, 2016 7:46 pm, edited 1 time in total.

Dann Corbit
Posts: 11621
Joined: Wed Mar 08, 2006 7:57 pm
Location: Redmond, WA USA
Contact:

Re: C# vs C++

Post by Dann Corbit » Wed Nov 09, 2016 7:36 pm

"Stockfish Port App 5.0" gives about 300K NPS, compared to about 1 M NPS for SF on one thread in C++ {opening position of chess}.

Portfish gives about 500K NPS.

Alfilx64 (another SF translation to C#) {despite the ghastly trick of setting thread priority to HIGH} gets under 250K NPS on one thread.

So the penalty is anywhere from 75% to 50% cost in NPS throughput.
Or a factor of 2x to 4x in speedup going to C++ from C#.

Of course, YMMV
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.

Jesse Gersenson
Posts: 584
Joined: Sat Aug 20, 2011 7:43 am
Contact:

Re: C# vs C++

Post by Jesse Gersenson » Wed Nov 09, 2016 7:38 pm

See the 'C plus' page on the chess programming wiki.

Found this link there, "C# Performance", http://www.talkchess.com/forum/viewtopic.php?t=42186

Dann Corbit
Posts: 11621
Joined: Wed Mar 08, 2006 7:57 pm
Location: Redmond, WA USA
Contact:

Re: C# vs C++

Post by Dann Corbit » Wed Nov 09, 2016 7:39 pm

CheckersGuy wrote:I dont want to open another thread for asking a smiliar strategy. How much worse (speed/elo) is a chess engine that's written entirely in java ?
All the interpreted languages will have about the same penalty compared to natively compiled.

With Java there is a native compiler, but it still runs GC from time to time, so it won't be as fast as C or C++ even when compiled.

A lot of it also comes down to the skill of the programmer.

I think that a lot of the gain for CFish and AsmFish is not from the language change so much as from using good techniques in hot spots.
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.

Dann Corbit
Posts: 11621
Joined: Wed Mar 08, 2006 7:57 pm
Location: Redmond, WA USA
Contact:

Re: C# vs C++

Post by Dann Corbit » Wed Nov 09, 2016 7:43 pm

Dann Corbit wrote:"Stockfish Port App 5.0" gives about 300K NPS, compared to about 1 M NPS for SF on one thread in C++ {opening position of chess}.

Portfish gives about 500K NPS.

Alfilx64 (another SF translation to C#) {despite the ghastly trick of setting thread priority to HIGH} gets under 250K NPS on one thread.

So the penalty is anywhere from 75% to 50% cost in NPS throughput.
Or a factor of 2x to 4x in speedup going to C++ from C#.

Of course, YMMV
I ran the wrong Alfil binary. I got 340K NPS on one thread when I ran the right one.
Taking ideas is not a vice, it is a virtue. We have another word for this. It is called learning.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.

Henk
Posts: 6550
Joined: Mon May 27, 2013 8:31 am

Re: C# vs C++

Post by Henk » Wed Nov 09, 2016 7:44 pm

I think 0-50% but I don't know.

User avatar
Dan Honeycutt
Posts: 5258
Joined: Mon Feb 27, 2006 3:31 pm
Location: Atlanta, Georgia

Re: C# vs C++

Post by Dan Honeycutt » Wed Nov 09, 2016 8:23 pm

CheckersGuy wrote:I dont want to open another thread for asking a smiliar question How much worse (speed/elo) is a chess engine that's written entirely in java ?
Comparing Bruja and Cupcake, C++ is not quite twice as fast as Java (.jar file).

And, thanks Dann for your info.

Best
Dan H.

sandermvdb
Posts: 160
Joined: Sat Jan 28, 2017 12:29 pm
Location: The Netherlands

Re: C# vs C++

Post by sandermvdb » Sun Feb 12, 2017 10:02 am

Dann Corbit wrote:
CheckersGuy wrote:I dont want to open another thread for asking a smiliar strategy. How much worse (speed/elo) is a chess engine that's written entirely in java ?
With Java there is a native compiler, but it still runs GC from time to time, so it won't be as fast as C or C++ even when compiled.
If you don't instantiate objects, the garbage collector is almost never triggered, which is one of the reasons you should use make/unmake move instead of new ChessBoard() when performing a move.

Furthermore, Java has so called intrinsic methods for low-level methods like Long.bitCount(), Long.numberOfTrailingZeros(), Math.max(), etc... When these methods are called, the JVM tries to perform the equivalent CPU instruction, if available. This really improves the performance.
Another advantage of the JVM is that you don't need to compile your binary for these CPU instructions, the JVM figures out which instructions are available.

Post Reply