How much faster is C++ code "supposed" to be with optimizations turned on?

Viewed 289

I have a program that runs in around 1 minute when compiling with g++ without any options. Compiling with -O3 however makes it run in around 1-2 seconds.

My question is whether it is normal to have this much of a speed up? Or is my code perhaps so bad, that optimization can take away that much time. Obviously I know my code isn't perfect but because of this huge speedup I'm beginning to think it's worse than I thought. Please tell me what the "normal" amount of speed up is (if that's a thing), and whether too much speed up can mean bad code that could (and should) be easily optimized by hand instead of relying on the compiler.

1 Answers

How much faster is C++ code “supposed” to be with optimizations turned on?

In theory: There doesn't necessarily need to be any speed difference. Nor does there exist any upper limit to the speed difference. The C++ language simply doesn't specify a difference between optimisation and lack thereof.

In practice: It depends. Some programs have more to gain from optimisation than others. Some behaviours are easier to prove than others. Some optimisations can even make the program slower, because the compiler cannot know about everything that may happen at runtime.

... 1 minute ... [optimisation] makes it run in around 1-2 seconds.

My question is whether it is normal to have this much of a speed up?

It is entirely normal. You cannot assume that you'll always get as much improvement, but this is not out of the ordinary.

Or is my code perhaps so bad, that optimization can take away that much time.

If the program is fast with optimisation, then it is a fast program. If the program is slow without optimisation, we don't care because we can enable optimisation. Usually, only the optimised speed is relevant.

Faster is better than slower, although that is not the only important metric of a program. Readability, maintainability and especially correctness are more important.

Please tell me ... whether ... code ... could ... be ... optimized by hand instead of relying on the compiler.

Everything could be optimized by hand, at least if you write the program in assembly.

... or should ...

No. There is no reason to waste time doing what the compiler has already done for you.


There are sometimes reasons to optimise by hand something that is already well optimised by the compiler. Relative speedup is not one of those reasons. An example of a valid reason is that the non-optimised build may be too slow to be executed for debugging purposes when there are real time requirements (whether hard or soft) involved.

Related