Why games made with C++ are more optimized?

529 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.

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