简体   繁体   中英

const vs non-const variable with no change in value once assign

In C++, if value of a variable never gets changed once assigned in whole program VS If making that variable as const , In which case executable code is faster? How compiler optimize executable code in case 1?

A clever compiler can understand that the value of a variable is never changed, thus optimizing the related code, even without the explicit const keyword by the programmer.

As for your second, question, when you mark a variable as const , then the follow might happen: the "compiler can optimize away this const by not providing storage to this variable rather add it in symbol table. So, subsequent read just need indirection into the symbol table rather than instructions to fetch value from memory". Read more in What kind of optimization does const offer in C/C++? (if any) .

I said might , because const does not mean that this is a constant expression for sure, which can be done by using constexpr instead, as I explain bellow.


In general, you should think about safer code, rather than faster code when it comes to using the const keyword. So unless, you do it for safer and more readable code, then you are likely a victim of premature optimization.


Bonus:

C++ offers the constexpr keyword, which allows the programmer to mark a variable as what the Standard calls constant expressions . A constant expression is more than merely constant.

Read more in Difference between `constexpr` and `const` and When should you use constexpr capability in C++11?


PS: Constness prevents moving, so using const too liberally may turn your code to execute slower.

In which case executable code is faster?

The code is faster in case on using const , because compiler has more room for optimization. Consider this snippet:

int c = 5;
[...]
int x = c + 5;

If c is constant, it will simply assign 10 to x . If c is not a constant, it depend on compiler if it will be able to deduct from the code that c is de-facto constant.

How compiler optimize executable code in case 1?

Compiler has harder time to optimize the code in case the variable is not constant. The broader the scope of the variable, the harder for the compiler to make sure the variable is not changing.

For simple cases, like a local variables, the compiler with basic optimizations will be able to deduct that the variable is a constant. So it will treat it like a constant.

if (...) {
    int c = 5;
    [...]
    int x = c + 5;
}

For broader scopes, like global variables, external variables etc., if the compiler is not able to analyze the whole scope, it will treat it like a normal variable, ie allocate some space, generate load and store operations etc.

file1.c
int c = 5;

file2.c
extern int c;
[...]
int x = c + 5;

There are more aggressive optimization options, like link time optimizations, which might help in such cases. But still, performance-wise, the const keyword helps, especially for variables with wide scopes.

EDIT:

Simple example

File const.C:

const int c = 5;
volatile int x;

int main(int argc, char **argv)
{
        x = c + 5;
}

Compilation:

$ g++ const.C -O3 -g

Disassembly:

5   {
6       x = c + 5;
   0x00000000004003e0 <+0>: movl   $0xa,0x200c4a(%rip)        # 0x601034 <x>
7   }

So we just move 10 (0xa) to x.

File nonconst.C:

int c = 5;
volatile int x;

int main(int argc, char **argv)
{
        x = c + 5;
}

Compilation:

$ g++ nonconst.C -O3 -g

Disassembly:

5   {
6       x = c + 5;
   0x00000000004003e0 <+0>: mov    0x200c4a(%rip),%eax        # 0x601030 <c>
   0x00000000004003e6 <+6>: add    $0x5,%eax
   0x00000000004003e9 <+9>: mov    %eax,0x200c49(%rip)        # 0x601038 <x>
7   }

We load c , add 5 and store to x .

So as you can see even with quite aggressive optimization (-O3) and the shortest program you can write, the effect of const is quite obvious.

g++ version 5.4.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM