Why games made with C++ are more optimized?

510 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

Generally speaking, higher level languages handle more things for you so you don’t have to worry about them, but they often do so in quite an inefficient way. For example, in C you need to specify exactly which data types you’re working with, so that when the compiler sees something like `x+y` it knows whether it’s adding two long ints, or two doubles, or whatever, and can generate the appropriate machine code. In python, types are dynamic, and when the interpreter sees `x+y` it needs to first look up what types these are and how addition is defined for them. This allows you to write very flexible code that can work with all manner of different data types – even ones that you might not be considering when you write the code, and even ones that are defined on-the-fly at runtime – but it can create substantial slowdown because these type lookups need to happen so often.

There are usually ways of optimizing code in higher level languages that enable you to achieve similar performance, but it can take quite a bit of work. For example in python you can convert time-critical sections of code into “C extensions”, which are essentially libraries for python written in C. There are even pieces of software such as Cython that can do this for you automatically.

Anyway, I think the reason why C++ is so widely used in game development has more to do with inertia than anything else. Game devs are used to working in C++, and libraries and tools for game development are mostly aimed at C++. Scientific computing also demands efficiency, but high-level languages such as python, Julia and MATLAB are popular in that domain, as is Fortran, which is a relatively low-level language comparable to C.

> But i heard C is faster than C++ also

Not really. C++ is essentially just C with lots of additional features – or at least it was when it was first created. Since then C has grown a few extra features that work differently from the C++ equivalents. But most C code is also valid C++ code, and should result in pretty much the same compiled code whether you compile it as C code or as C++ code.

But many of the additional features in C++ are of the kind where the compiler is handling things automatically for you in a way that might not be the most efficient. So performance-critical code is often written in pure C, or in C++ but avoiding many C++-specific features.

Anonymous 0 Comments

I believe it’s got something to do with memory management and allocation. Using its different pointers to add/delete/edit data sets in the most efficient way.

Anonymous 0 Comments

It is not universally true. However the closer the programing language is to the machine code which actually runs on the processor the more control the programmer have over the code which allows them to write more optimized code. It is things like not following conventions when it would hinder performance, taking minor shortcuts, understanding how different code will get executed in the processor and therefore how it will perform, etc. It does not mean that all C and C++ code will be fast. In fact it takes longer to write code in these languages so there may not be enough time in the project to do other larger performance improvements. And more modern compiler for the higher end languages like Java and C# can actually get better insight into the code then the developer and may do more performance optimization then most people will do on their own. Most AAA titles is written in a number of different languages depending on which works best for the given scenarios. The core of the game engine might be written in highly optimized C++ code that have been perfected over many years and is used for several titles. But the world interactions, animations and scripts are usually written in their own high level language specific to that game engine. Even things like the AI might be written in a completely separate language which is designed to make it quick and easy to work with the AI.

Anonymous 0 Comments

Python is interpreted, not compiled, so the interpreter is effectively turning the source code into machine code as you run the program. This slows it down a whole lot, and other interpreted languages have the same issue. With C or C++, you compile the code into machine code, so you only do that work once per platform, instead of every time you run the program.

Now, you can write something in C and C++, and have them both the same speed, or one faster than the other, depending on what compilers and settings you use, because modern compilers are really good at optimizing code down to equivalent instructions that run faster. Which one ends up faster usually is a question of how clever the compiler is rather than anything inherent to the language.

Anonymous 0 Comments

C/C++ is a language built on the assumption that the developers will handle EVERY aspect of their program, this includes aspects as low level as managing memory and basic I/O(2 huge areas where bottlenecks in speed are common), if you ignore to handle any of these your program will not run at all or it just crashes when it encounter an unexpected situation, this increases complexity but because everything is managed closely you cna fine tune its performance to what you want it to be

other languages like C# and Python implement automated systems to deal with some of this basic tasks so that the developer doesn’t need to, but these systems are not specialized for all needs(or in some cases handling them is mandatory even if the program doesn’t use them) so performance is not the best it can be.

as a bonus both C and C++ have the ability to indent code from other languages most notably Assembly listings, this enables a savvy developer to implement code that is accessed VERY frequently(and hence could become a bottleneck) in Assembly in order to make sure this code is optimized at the lowest possible level before actual machine code.

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.

Anonymous 0 Comments

Imagine you are writing a recipe for a Pie. In assembly you would write “take 5 steps to the fridge, open fridge with your right hand, grab an egg…” and it would depend on your exact kitchen. In C/C++ you would write “Get an egg from the fridge.” In Python you would write “make a pie crust” and you leave the interpreter to figure it out at run time, which is going to take time.

Anonymous 0 Comments

C++ is a relatively low level programming language that is ultimately compiled down to raw machine code. It is highly versatile, but also has a large learning curve and makes it very easy to do something wrong. That being said, if a developer knows what they’re doing, they can make an extremely well optimized program in C++. Higher level languages such as C#, python, etc… are all mostly designed for ease of development and practicality. They handle things like memory, garbage collection, system calls, things along those lines. 99% of the time, they are **efficient enough**, but will not run the same as a low level language.