简体   繁体   中英

C++ local variables, performance

I was just wondering what happens when variable in function scope is always the same and don't change, for example

void test()
{
    int x = 10;
}

I know that compiler stores informations about its name, size etc. but in this case will it cache its value aswell?

My thoughts was if that was a member function that was used like 60 or 140 times per sec. what impact on performance it would have, should it be moved as a member variable to improve performance?

Except for debug symbols: No, compiler typically doesn't store names of local variables.

Local automatic variables rarely have overhead compared to achieving the same without those variables. To verify whether this is the case in your program, you can measure.

In case of the example function, it is probably optimised into a no-op and there will be no trace of the local variable left.

Member variables cannot be optimised away typically, so adding those unnecessarily can easily affect performance. Whether the effect is significant, you can find out by measuring.

Like mentioned in comments if you inspect the assembly generated for such code you will see that the variable gets optimized away (won't even exist in any sense).

If you were to have a function like:

int test()
{
    int x = 10;
    return x;
}

there would not be a variable created either but the compiler would simply have your function return 10.

As for the points about performance unless you already have a measurable performance problem I would not worry at all about "where" your constant is stored.

You will have a very hard time measuring any performance impact.

Helpful links to reasearch this include:

https://godbolt.org/ to try out your code and see the assembly generated (remember to set the compiler flags)

This excelent answer on measuring performance in code: https://stackoverflow.com/a/60293070/567070

How well this is optimized depends on your compilation settings. godbolt.org allows you to compile code and look at the compiled output. In this example, gcc9.2 compiles your code differently depending on the optimization flags.

No optimization:

https://godbolt.org/z/Ej6ado

The local variable pushed onto the stack, then popped from the stack immediately.

Lowest possible optimization with -O1:

https://godbolt.org/z/dnesvs

The variable mention in your code is completely optimized away, since it isn't used: The function does nothing and returns immediately.

It may be detrimental to make the variable a member variable, since that would in theory allow for it to change between function calls and even during execution of the function. If it is a constant variable, just declare it as 'const' in your funtion and don't worry about optimizations.

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