What's the approximate speed difference (if any) for a chess program written in C# and C++?
Thanks
Dan H.
C# vs C++
Moderators: hgm, Rebel, chrisw
-
- Posts: 273
- Joined: Wed Aug 24, 2016 9:49 pm
Re: C# vs C++
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.
-
- Posts: 12541
- Joined: Wed Mar 08, 2006 8:57 pm
- Location: Redmond, WA USA
Re: C# vs C++
"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
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.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
-
- Posts: 593
- Joined: Sat Aug 20, 2011 9:43 am
Re: C# vs C++
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
Found this link there, "C# Performance", http://www.talkchess.com/forum/viewtopic.php?t=42186
-
- Posts: 12541
- Joined: Wed Mar 08, 2006 8:57 pm
- Location: Redmond, WA USA
Re: C# vs C++
All the interpreted languages will have about the same penalty compared to natively compiled.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.
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.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
-
- Posts: 12541
- Joined: Wed Mar 08, 2006 8:57 pm
- Location: Redmond, WA USA
Re: C# vs C++
I ran the wrong Alfil binary. I got 340K NPS on one thread when I ran the right one.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
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.
But sharing ideas is an even greater virtue. We have another word for this. It is called teaching.
-
- Posts: 7220
- Joined: Mon May 27, 2013 10:31 am
Re: C# vs C++
I think 0-50% but I don't know.
-
- Posts: 5258
- Joined: Mon Feb 27, 2006 4:31 pm
- Location: Atlanta, Georgia
Re: C# vs C++
Comparing Bruja and Cupcake, C++ is not quite twice as fast as Java (.jar file).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 ?
And, thanks Dann for your info.
Best
Dan H.
-
- Posts: 160
- Joined: Sat Jan 28, 2017 1:29 pm
- Location: The Netherlands
Re: C# vs C++
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.Dann Corbit wrote: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.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 ?
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.