c# have lower perfomrance than c++?

is true that c++ is better for games because is like less heavy ?

(I know you’re not going to like this, but…)

It depends.

2 Likes

some ex xd ?, bbut in most time which is bebtter? because i see that 80% of games are made with c++ and most of AAA too and i dont know why, if c# is more modern right ?

In theory, C++ is faster because it allows you to have more control over exactly what’s happening, from memory management to, well, everything.

In reality, it mostly doesn’t matter anymore because most of the time the performance difference is so insignificant that it’s often not worth bothering about.

You can also apply this logic to assembly programming. In theory, ASM is faster than C++.

So why aren’t we coding in ASM anymore?

Because we’re far more productive in C++.

By that similar logic, I’ve largely ditched C++ in favour of C# because I can get stuff done in C# so much more quickly than in C++.

3 Likes

Because a lot of those AAA games are done for consoles, and you’re going to want to squeeze every last bit of performance you can out of those consoles. Which means explicitly handling the memory yourself, using pools, etc.

Of course, those AAA games also have teams with dozens if not hundreds of people all working to eke out that last bit of power.

ty

C# is generally compiled into an intermediate language that is then converted into machine code on the fly when your player runs your game, but when you compile with C++ you end up with assembly or directly into machine code for your target platform. So there is no IL to machine code compilation that occurs when running a program written in C++.

Unity though has done something clever with some of their target platforms with their IL2CPP tool, which effectively generates C++ code from your C# code, which is then compiled like C++ code usually is. This in theory gives C++ level performance to C#. Unfortunately Win/Mac standalone are not yet on the supported platform list.

Another performance difference is garbage collection. C# is a managed language, meaning you don’t have to manually allocate and release memory and keep track of what memory is in use to avoid memory leaks. The garbage collector generally handles this for you automatically. Though this isn’t completely free performance wise. C++ on the contrary generally requires that the developer manually release memory when it is no longer in use. This is better performance wise, but is easy to screw up, and leads to far more complicated code for even relatively simple tasks.

In the end though where is your performance bottleneck? All this theoretical performance gain of C++ is all on the CPU, but when was the last time you played a game with your CPU pegged at 100% while your GPU was sitting there with extra cycles? If your GPU is the bottleneck then you’re not losing much using C# over C++, and the speed at which you can code in C# and avoid issues like memory leaks is probably a no brainer trade off. At least it is for an indie dev like me.

Chances are you won’t be writing a AAA game by yourself, so it’s kind of a mute point. C# is more sophisticated, so it will keep you from making huge mistakes that leak memory, etc, at least as often, but that’s going to have a cost. If someone is making a huge game with high detail, they will probably choose Unreal and c++. It will probably take longer to program and debug, but they are working in teams. A lot of people really like c++, so it might be something you want to look at. It’s kind of if you want to get the work done in the least possible time, or you want to spend more time but have a game that could run a little faster. The nice thing is, they aren’t that different, so it will still be easier to learn c++ if you know c#, and vice versa. The main thing is to get programming.

I find that as a developer working on my own projects I way prefer higher level languages like c# to lower level languages like c++. Using C# in Unity lets me have much quicker iteration times, since I’m not waiting a few minutes for a build.

2 Likes