C# vs C++

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

Moderators: hgm, Rebel, chrisw

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

C# vs C++

Post by Dan Honeycutt »

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 9:49 pm

Re: C# vs C++

Post 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 ?
Last edited by CheckersGuy on Wed Nov 09, 2016 8:46 pm, edited 1 time in total.
Dann Corbit
Posts: 12540
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: C# vs C++

Post 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
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: 593
Joined: Sat Aug 20, 2011 9:43 am

Re: C# vs C++

Post 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
Dann Corbit
Posts: 12540
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: C# vs C++

Post 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.
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: 12540
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: C# vs C++

Post 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.
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: 7218
Joined: Mon May 27, 2013 10:31 am

Re: C# vs C++

Post by Henk »

I think 0-50% but I don't know.
User avatar
Dan Honeycutt
Posts: 5258
Joined: Mon Feb 27, 2006 4:31 pm
Location: Atlanta, Georgia

Re: C# vs C++

Post 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.
sandermvdb
Posts: 160
Joined: Sat Jan 28, 2017 1:29 pm
Location: The Netherlands

Re: C# vs C++

Post 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.