Page 1 of 1

C# vs C++

Posted: Wed Nov 09, 2016 6:32 pm
by Dan Honeycutt
What's the approximate speed difference (if any) for a chess program written in C# and C++?

Thanks
Dan H.

Re: C# vs C++

Posted: Wed Nov 09, 2016 8:34 pm
by CheckersGuy
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 ?

Re: C# vs C++

Posted: Wed Nov 09, 2016 8:36 pm
by Dann Corbit
"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

Re: C# vs C++

Posted: Wed Nov 09, 2016 8:38 pm
by Jesse Gersenson
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

Re: C# vs C++

Posted: Wed Nov 09, 2016 8:39 pm
by Dann Corbit
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.

Re: C# vs C++

Posted: Wed Nov 09, 2016 8:43 pm
by Dann Corbit
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.

Re: C# vs C++

Posted: Wed Nov 09, 2016 8:44 pm
by Henk
I think 0-50% but I don't know.

Re: C# vs C++

Posted: Wed Nov 09, 2016 9:23 pm
by Dan Honeycutt
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.

Re: C# vs C++

Posted: Sun Feb 12, 2017 11:02 am
by sandermvdb
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.