[Solved] Why C/C++ is slower than Assembly and other low level languages?


The amount of time required to execute the core of your program you’ve written is incredibly small. Figure that it consists of three or four assembly instructions, and at several gigahertz that will only require a couple of nanoseconds to run. That’s such a small amount of time that it’s vastly below the detection threshold for the time program, whose resolution is measured in milliseconds (remember that a millisecond is a million times slower than a nanosecond!) So in that sense, I would be very careful about making judgments about the runtime of one program as being “twice as fast” as the other; the resolution of your timer isn’t high enough to say that for certain. You might just be seeing noise terms.

Your question, though, was why there is all this automatically generated code if nothing is going to happen. The answer is “it depends.” With no optimization turned on, most compilers generate assembly code that faithfully simulates the program you wrote, possibly doing more work than is necessary. Since most C and C++ functions, you actually will have code that does something, will need local variables, etc., a compiler wouldn’t be too wrong in emitting code at the start and end of a function to set up the stack and frame pointer properly to support those variables. With optimization turned up to the max, an optimizing compiler might be smart enough to notice that this isn’t necessary and to remove that code, but it’s not required.

In principle, a perfect compiler would always emit the fastest code possible, but it turns out that it’s impossible to build a compiler that will always do this (this has to do with things like the undecidability of the halting problem). Therefore, it’s somewhat assumed that the code generated will be good – even great – but not optimal. However, it’s a tradeoff. Yes, the code might not be as fast as it could possibly be, but by working in languages like C and C++ it’s possible to write large and complex programs in a way that’s (compared to assembly) easy to read, easy to write, and easy to maintain. We’re okay with the slight performance hit because in practice it’s not too bad and most optimizing compilers are good enough to make the price negligible (or even negative, if the optimizing compiler finds a better approach to solving a problem than the human!)

To summarize:

  • Your timing mechanism is probably not sufficient to make the conclusions that you’re making. You’ll need a higher-precision timer than that.

  • Compilers often generate unnecessary code in the interest of simplicity. Optimizing compilers often remove that code, but can’t always.

  • We’re okay paying the cost of using higher-level languages in terms of raw runtime because of the ease of development. In fact, it might actually be a net win to use a high-level language with a good optimizing compiler, since it offloads the optimization complexity.

3

solved Why C/C++ is slower than Assembly and other low level languages?