Do const
declarations help the compiler (GCC) produce faster code or are they only useful for readability and correctness?
Zed Shaw has argued that const
is useless or is overused in C/C++:
Next is all the bizarre fascination with const. For some odd reason C++ loves to make you slap const on every part of a declaration, and yet I get the same end result as C: a function is called. (...)
(From: http://librelist.com/browser//mongrel2/2010/7/15/c-verses-c++/#770d94bcfc6ddf1d8510199996b607dd )
Only const on an object definition actually makes it immutable. The main point of using const is not to assist the compiler in optimizations but to protect yourself from mistakes.
If the value is a compile time constant (e.g. numbers, enum , const values, constexpr sometimes in c++11 and so on), then yes they can be accessed faster compared to other variables. They can even be placed in the code segment.
Using -march=native enables all instruction subsets supported by the local machine (hence the result might not run on different machines). Using -mtune=native produces code optimized for the local machine under the constraints of the selected instruction set.
-O2 Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff. The compiler does not perform loop unrolling or function inlining when you specify -O2. As compared to -O, this option increases both compilation time and the performance of the generated code.
Yes. Here’s one concrete example. const
makes it possible to pass arguments by const&
rather than by value (which might require a costly copy). It’s important to realise that the alternative to pass-by-const&
is not pass-by-&
because the latter doesn’t allow temporaries to be bound. So, for instance, this code:
auto result = foo{1} + foo{2} + foo{3};
may call foo operator +(foo const&, foo const&)
but it may not call foo operator +(foo&, foo&)
.
That way, const
helps avoid copies.
But generally, const
is a tool to ensure correctness, not to to aid optimisations.
Either way, Zed Shaw has no idea what he’s talking about. The rest of his rant is similarly misinformed, by the way.
No, const
does not help the compiler make faster code. Const
is for const-correctness, not optimizations.
The C++ standard says that const
items can't be modified, but also says that const_cast
should remove the const
modifier from an object and make it writable (unless it's located in actually read-only memory, in which case the behavior is undefined); as such const
cannot mean, in general, that the target variable will not change.
I can only think of these two very narrow scenarios where having const
produces faster code than not having it:
static
) and is passed by reference or pointer to a function defined in a different translation unit (different file). In this case, the compiler cannot elide reads to it if it is not marked const
;extern
). Reads to a const extern
can be elided inside the file that defines it (but nowhere else).When const
is applied to a global variable, the compiler is allowed to assume that the value will never change because it will place it in read-only memory, and this means undefined behavior if the program attempts to modify it, and compiler authors love to rely on the threat of undefined behavior to make code faster.
Note that both scenarios apply only to global variables, which probably make for a very minor portion of the variables in your program. To its merit, however, const
implies static
at the global level in C++ (this is not the case in C).
Someone above said that using const
can make code faster because it's possible to use const
references. I would argue here that what make the code faster is the use of a reference, not the use of const
.
That said, I still believe const
is a very sharp knife with which you can't cut yourself and I would advise that you use it whenever it's appropriate, but don't do it for performance reasons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With