Why C++ instead of C#?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Chessnut1071
Posts: 313
Joined: Tue Aug 03, 2021 2:41 pm
Full name: Bill Beame

Re: Why C++ instead of C#?

Post by Chessnut1071 »

Henk wrote: Tue Aug 31, 2021 2:33 pm I used C++ in the past. I liked .h files. I use C# now but I don't see a reason to go back.
Maybe anyone going back to C++ can answer the question why use C++ instead of C# ?
Before you can answer that, you need to give some indication of the speed increase by going to C++ over C#. I heard examples of 2x faster using C++; however, you need a lot more than that even for a 1-ply increase [without pruning].
User avatar
lithander
Posts: 880
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: Why C++ instead of C#?

Post by lithander »

Chessnut1071 wrote: Thu Sep 16, 2021 3:21 pm I heard examples of 2x faster using C++; however, you need a lot more than that even for a 1-ply increase [without pruning].
And I doubt the differerence is even that much. But I'd like to have a proof for that gut-level hunch of mine and so I'm going to port some C++ movegen code that relies heavily on intrinsics and bitfiddling over to C# and then we can compare the speed.

I was thinking of using https://sites.google.com/site/pedoneche ... tic-engine as a basis because that's really full of stuff that C++ is supposedly very good in and C# very bad. E.g. Pedro says in this thread
pedrojdm2021 wrote: Wed Sep 15, 2021 4:54 am In my experience in C# ( . net framework 4.6.1 ) even doing bitwise operations just like the algorithm that appears in chessprogramming wiki:
https://www.chessprogramming.org/Square ... all_Pieces

That thing is very slow in C# at least in the C# that i'm running, you can't call that thing ~6M of times because it will be a very big slowdown. in C++ that should be really fast as you are just doing bitwise operation that are known to be super fast instructions
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
User avatar
lithander
Posts: 880
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: Why C++ instead of C#?

Post by lithander »

The QBBEngine already comes with a few positions to run a perft test on:

Code: Select all

            TestPerft("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", 6, 119060324); //Start Position
            TestPerft("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1",5,193690690);
            TestPerft("8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1", 7,178633661);
            TestPerft("r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1",6,706045033);
            TestPerft("rnbqkb1r/pp1p1ppp/2p5/4P3/2B5/8/PPP1NnPP/RNBQK2R w KQkq - 0 6",3,53392);
            TestPerft("r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10",5,164075551);
At the moment I have all the code ported to C# that is needed to run these perft tests and I skipped the alpha-beta search related rest.
Preliminary results have C++ only about 30% faster than C#!

C++

Code: Select all

 4448 ms, 26767 knps (+28%)
 6487 ms, 29858 knps (+31%)
 7154 ms, 24969 knps (+29%)
25245 ms, 27967 knps (+33%)
    2 ms, 26696 knps (+28%)
 5511 ms, 29772 knps (+28%)
C#

Code: Select all

 5711 ms, 20847 knps
 8539 ms, 22682 knps
 9270 ms, 19269 knps
33632 ms, 20992 knps
    2 ms, 21808 knps
 7080 ms, 23173 knps
Some notes:
  • I made no C# specific optimizations when porting. I tried to have the code look as identical as possible. That means some expression bodied properties (where there previously where pointers or macros that I can only hope the runtime knows how to inline! ;)
  • I didn't profile anything yet, so there's probably room for making the C# version faster.
  • There's some minor(?) stuff left out from the C# version of the movegenerator that is not needed for perft. To be absolutely fair I need to make the same perft-only modifications to the C++ code also.
  • In C# I used a few more hardware intrinsics because they were conveniently available. Maybe it's fair to not use them? Gotta test how much the difference even is.
  • C# version uses .Net 5 runtime. C++ version is compiled via "gcc QBBEngine.c -o qbb.exe" and because I'm on Windows I use MinGW 64bit
  • Measurements were done on a Ryzen 3600 @ 4.2Ghzz (fixed clockrate) running both executables side by side.
I'll probably make the two versions more equal and measure again. But for now I'd say it doesn't look like any C# developer should abandon his language out of performance concerns.
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess
R. Tomasi
Posts: 307
Joined: Wed Sep 01, 2021 4:08 pm
Location: Germany
Full name: Roland Tomasi

Re: Why C++ instead of C#?

Post by R. Tomasi »

lithander wrote: Fri Sep 17, 2021 2:40 am At the moment I have all the code ported to C# that is needed to run these perft tests and I skipped the alpha-beta search related rest.
Preliminary results have C++ only about 30% faster than C#!
Thank you for taking the time to implement and run those tests. Very interesting. I have to admit, I am somewhat surprised (would have expected the difference to be even lower).
lithander wrote: Fri Sep 17, 2021 2:40 am There's some minor stuff left out from the C# version of the movegenerator that is not needed for perft. To be absolutely fair I need to make the same perft-only modifications to the C++ code and remove that stuff too.
Looking forward to these more accurate results!
lithander wrote: Fri Sep 17, 2021 2:40 am In C# I used a few more hardware intrinsics because they were conveniently available. Maybe it's fair to not use them? But it's not a practical change.
I'm inclined to say that's fair game, since you would use any intrinsics available in a real-world setting, too.
lithander wrote: Fri Sep 17, 2021 2:40 am C# version uses .Net 5 runtime. C++ version is compiled via "gcc QBBEngine.c -o qbb.exe" and because I'm on Windows I use MinGW 64bit
If possible, I would love to see a comparision with Clang, since in my experience it does a better job at optimizing C++ than gcc. I guess it's fairer to compile against the compiler that's best at optimizing. As for the C# part: I assume the assembly has not been precompiled (there is a possibility to precompile, so that it does not happen JIT, afaik)?
User avatar
j.t.
Posts: 239
Joined: Wed Jun 16, 2021 2:08 am
Location: Berlin
Full name: Jost Triller

Re: Why C++ instead of C#?

Post by j.t. »

lithander wrote: Fri Sep 17, 2021 2:40 am ...
I am not sure, but it seems to me that your C++ times are too high. On my notebook with an i5-8250U (which should be slower than your Ryzen IIRC), it the perft test takes a total of 42 seconds, while your numbers show 48 seconds. The flags I used are "clang -Ofast -flto -DNDEBUG main.c", if I add " -march=native " it even goes down to 26 seconds. Of course these are all very rough measurements and assumptions, but I would agree with Roland, that it may be a good idea to compiler with "clang -Ofast -flto -DNDEBUG main.c" instead of mingw (the "-flto" won't matter because it's just a single file but this flag may help immensely when using more than one .c file) (for clang to work on windows you need to install "LLVM-12.0.1-win64.exe" from here: https://github.com/llvm/llvm-project/re ... org-12.0.1, and have some msvs compiler tools installed (usually comes with Visual Studio 2019).
Mergi
Posts: 127
Joined: Sat Aug 21, 2021 9:55 pm
Full name: Jen

Re: Why C++ instead of C#?

Post by Mergi »

I think you need to turn on compiler optimizations, otherwise this hardly seems fair. Even on my waaaay slower processor than you have, i'm getting 32m+ NPS on startpos. Compiling on windows with MinGW 10.2 with just -O3 and nothing else.
klx
Posts: 179
Joined: Tue Jun 15, 2021 8:11 pm
Full name: Emanuel Torres

Re: Why C++ instead of C#?

Post by klx »

This is not a fair comparison at all. You need to turn on both compiler optimizations and native architecture (-Ofast -march=native), since that's what C# would be doing. And yes, clang is preferred over gcc.

Without these flags, on my much slower machine, I get:

Code: Select all

 5243 ms, 22706 knps
 7992 ms, 24236 knps
 8257 ms, 21635 knps
29861 ms, 23645 knps
    3 ms, 17289 knps
 6963 ms, 23565 knps
With the flags:

Code: Select all

 1952 ms, 60984 knps
 2708 ms, 71525 knps
 2959 ms, 60368 knps
10580 ms, 66737 knps
    1 ms, 41870 knps
 2345 ms, 69977 knps
So, my preliminary results are: C++ is 3.7x faster than C#
[Moderation warning] This signature violated the rule against commercial exhortations.
pedrojdm2021
Posts: 157
Joined: Fri Apr 30, 2021 7:19 am
Full name: Pedro Duran

Re: Why C++ instead of C#?

Post by pedrojdm2021 »

klx wrote: Fri Sep 17, 2021 4:41 am This is not a fair comparison at all. You need to turn on both compiler optimizations and native architecture (-Ofast -march=native), since that's what C# would be doing. And yes, clang is preferred over gcc.

Without these flags, on my much slower machine, I get:

Code: Select all

 5243 ms, 22706 knps
 7992 ms, 24236 knps
 8257 ms, 21635 knps
29861 ms, 23645 knps
    3 ms, 17289 knps
 6963 ms, 23565 knps
With the flags:

Code: Select all

 1952 ms, 60984 knps
 2708 ms, 71525 knps
 2959 ms, 60368 knps
10580 ms, 66737 knps
    1 ms, 41870 knps
 2345 ms, 69977 knps
So, my preliminary results are: C++ is 3.7x faster than C#
That are some interesting stuff, looking forward for the final results :D
R. Tomasi
Posts: 307
Joined: Wed Sep 01, 2021 4:08 pm
Location: Germany
Full name: Roland Tomasi

Re: Why C++ instead of C#?

Post by R. Tomasi »

j.t. wrote: Fri Sep 17, 2021 3:54 am for clang to work on windows you need to install "LLVM-12.0.1-win64.exe"
I assume lithander is using Visual Studio (since he's writing C# on Windows): Clang is bundled with VS, you can install it via the VS Installation manager - no need to install anything on top of VS.
User avatar
lithander
Posts: 880
Joined: Sun Dec 27, 2020 2:40 am
Location: Bremen, Germany
Full name: Thomas Jahn

Re: Why C++ instead of C#?

Post by lithander »

Thanks for the feedback on how to properly compile the C source. I'll give it a try after work.

It's good to know that my C build wasn't running at full speed because frankly I expected a larger difference as my focus so far was to get the C# version to compile and correctly compute perft and there are a few things in there that I would have expected to hurt the performance. That the builds were so close performance wise form the start came as a big surprise to me. Now I know why.

As so many of you seem interested in the topic I'm thinking about sharing the C# version with you so everyone who wants can replicate the measurements. We could then also try to make the version faster together, leverage the full potential of C# so to speek. Learn more about what micro-optimizations are possible in C# is one of the reasons I undertook this little experiment.

The only thing that's stopping me at the moment (apart from being at work^^) is that I don't know under what terms the author of the QBBEngine meant to share his code. Didn't find an open source license mentioned anywhere specifcally. On the other hand if someone is sharing the code of a didactic engine I assume he expects others to interact with it in some form?
Minimal Chess (simple, open source, C#) - Youtube & Github
Leorik (competitive, in active development, C#) - Github & Lichess