Why games made with C++ are more optimized?

525 views

C++ is the language used in almost all AAA titles. I seen youtube tests that show C++ to execute tasks faster than C# or Python for example. But i heard C is faster than C++ also, so why C++?. And what makes C or C++ faster than other languages (except assembly and machine code of course)?

In: Technology

8 Answers

Anonymous 0 Comments

C++ allows things that are hidden from the programmer in higher-level languages. For example raw memory access, better control over instruction execution, or disabling exception handling.

Raw memory access can be used in a couple of ways. For example to clear memory used for storing floating-point (real) numbers with integer zeros if this operation is faster than storing floats on your machine. Or to allocate a large chunk of memory at the launch and quickly serve smaller portions of it instead of asking the system to find another free chunk on demand. Or to place objects in continuous memory block so sequential access to these objects is faster due to how memory access works. Or to free unused memory when the program is doing less work (on loading screens) and the user won’t notice slow downs.

Control over instruction execution can mean a couple of things, but it boils down to situations where the program needs to jump to another place in code for some reason – there’s a function call, a conditional statement, or a loop. Compilers and processors are pretty good in optimizing jumps, but if they fail to predict what should happen, it can be costly. The programmer usually knows their code better and can sometimes guide the compiler on how to compile the code better (for example to remove jumps), but only if the language has the tools for that, which C++ has.

The exception handling example is about checking for situations where something can go wrong. Java, for example, forces programmers to use its built-in mechanism of exceptions for every single piece of code. This mechanism in general is nice and very generic, but again, the programmer may know their code well enough to say that certain errors cannot happen. For example a file cannot be empty, because the programmer just created it. Java will generate a piece of exception handling code anyway, but with C++ the programmer can skip it.

You are viewing 1 out of 8 answers, click here to view all answers.