

Ask HN: What are the performance gains using native C++ instead of C#? - z3phyr

Especially in video games
======
UnoriginalGuy
Depends on the programmer.

Both C# and C++ can be used to make a video game, they can be used to make a
3D video game. Heck you could almost certainly write a well selling commercial
game in C#. I mean Minecraft was written in Java and look how well that has
done...

The reason people like C++ is because you can optimise it down to near its
optimal speed at key choke points in the code. It is kind of like the 95%
rule, 95% of the time C++ and C# will run at similar speeds, but it is that 5%
where C++'s versatility really matters.

~~~
z3phyr
But minecraft dosen't look photorealastic; I mean, the engines like luminous,
unreal engine 4, id tech 5.

Can C# or Java be optimised to run these kinds of codebases with near similar
efficiency?

~~~
UnoriginalGuy
The thing about these large industrial game engines is that they're build from
very efficient libraries already written in C/C++. So the optimisation has
already happened.

You could write a "game" in C# that uses the native broker to call into the
C/C++ libraries and generate something remotely similar to what a modern game
engine does, but there will always be overhead in doing so.

Efficiency isn't really an interesting discussion topic in this area to be
honest. What is interesting is the fact that almost all of the code already
written for games is in C/C++.

You want to use a library? It will be C/C++. You want full access to DirectX?
C/C++. You want access to all of the OS's underlying APIs/data-structures?
C/C++.

Even if C# was entirely as efficient as C/C++ (and as I said 95% of the time
it really is) - it doesn't actually matter, because for the last twenty years
programmers have been pumping out game libraries for C/C++, not for C#.

C# and many other languages have a chicken/egg problem: Nobody wants to write
game code for the language because nobody is writing game code for the
language.

~~~
z3phyr
Is there any possibility of the video game industry market shifting toward C#
in forseeable future?

I emailed Aras, lead graphics programmer at Unity technology. He replied that
C++ in video game industry, esp in game engines is indispensible, and it will
remain the standard in long forseeable future.

I also sent an email to Mike Abrash at valve, and he replied that there is no
movement away from C++.

~~~
UnoriginalGuy
> Is there any possibility of the video game industry market shifting toward
> C# in forseeable future?

No chance at all.

It offers nothing that C/C++ doesn't already. It has no game libraries written
for it at all. It is largely unsupported on non-Windows platforms (e.g.
Android, Linux, Mac, etc).

Best case scenario: The XBox 720 allows "apps" to be written in C# and sold to
end-users. So C# becomes the de-facto indie game language of the XBox 720 (as
in mini-games, not full AAA titles).

------
eduardordm
The problem with C# and Java, among a number of things, is that you can't
implement some LOD algorithms without adding considerable GC overhead.
Example: <https://graphics.llnl.gov/ROAM/roam.pdf>

Modern video games will use all extensions available, this can't be done
directly in C# nor Java, you need some native code to do that. Example:
<http://glew.sourceforge.net/>

Also, you want to make you game portable, if you write a physics engine in C++
you could run it the it is on iOS, Android, Windows, Mac and most consoles,
try that with C# or Java.

Th use of state-of-art algorithms and extensions provided by gfx card
manufacturers add zillion-folds of performance gains. The difference is not on
frame rates but complete impossibility of doing some more advanced effects
without those extensions. If you are building something like minecraft, you
should be just fine on C#, Java and maybe JavaScript. The trend now seems to
be less focused on graphics and more on the fun itself.

People also use C/C++ because everybody does and you probably learned game
development using GLUT or something like that.

:D

(edited last paragraph for clarity)

------
eddiemunster
Assuming that you're fluent in both languages to the same level And that
you're talking about raw performance (and therefore ignoring anything to do
with development time, not having to track down dangling pointer bugs, having
multi platform codebases, individual platform support etc).

\- There'll be little or no perf difference for GPU between the 2 as the
underlying drivers will be the same. \- C# can call c++ which can in turn call
assembly so if you really need bare to the bones performance you could
probably manage it....

So really...for me it really comes down to garbage collection, currently you
have no real control over when it happpens and how long it takes; and that
really is a killer in games. If you have 16ms to render a frame and then
suddenly GC kicks in for 10ms then you might as well pack up your bag and go
home.

If I could specify when and how long the GC goes for with some sense of
control over it then I'd love to use C#.

But without that control it's a pain in the bottom.

------
BSousa
Unreal Engine uses UnrealScript. No idea about id tech. Unity is c#.

There are speed differences, but this is greatly dependent on what you are
doing and unless you are Epic or iD, I doubt it will matter. And if you are
asking this question, I really doubt you will be able to use C++ to get the
optimisations those engines have.

You can take a look at <http://benchmarksgame.alioth.debian.org> to get an
idea, c# in the mono runtime is about 3-4 times slower than C (from other
benchmarks, the mono runtime is slower than the microsoft one)

~~~
z3phyr
Unreal Engine is writern in C++. So is Unity3d. They just provide an
abstraction layer to US and C# respectively....

~~~
BSousa
Yes, and as I mentioned, I really doubt you are at the level where those
performance gains matter. If you use c# you will be working with DirectX,
which most means much of the heavy lifting is done there. Unreal and Unity
have teams of programmers working trying to squeeze the last bits of
performance (Unreal winning there for sure) of every platform. Which leads to
my other point. I think the main reason for anyone (unless we are talking 50+
people team on the engine) to use c++ is portability NOT speed.

------
TheCoelacanth
Other people have discussed the speed, but one aspect of performance that
people often ignore is memory usage. In C# (or at least the most common
implementation), there is a per object overhead of at least 8 bytes on 32-bit
platforms and 16 bytes on 64-bit platforms. C++ implementations typically have
no overhead for objects without virtual functions and 4 or 8 bytes of overhead
for objects with virtual functions. This means that a program written in C++
could potentially use a much lower amount of memory than one written in C#.

------
mbrodersen
The advantage of C++ is that you can completely control memory layout. Because
of the way modern CPU's work, making sure that data is aligned to minimize
cache misses is super important when writing high performance code. But it is
only important for maybe 5% of the code (the low level vertex and texture
management code). The rest you can write in _almost_ any language.

