Does creating local variables all the time generate extra cost for the software?

Asked

Viewed 112 times

11

I made a program of which he writes variables all the time, and with that generated me a doubt, instantiating a variable all the time generates more cost than just assigning the value to an existing variable?

Examples:

Instantiating a variable all the time:

while (true)
{
    int x = 10;
    int y = 30;
    int z = x + y;
}

Using an existing variable:

int x, y, z;
while (true)
{
    x = 10;
    y = 30;
    z = x + y;
}

These examples are just to improve the illustration of the problem. The program from which I will apply this is much larger and uses many more variables.

I thought I’d seen a question like that some time ago, but today I needed that answer, I couldn’t find it. If you find it, feel free to mark this question as duplicated.

1 answer

10


In this example it is the same, has example that there is an important semantic change and the code will give different results.

But speaking of cost, it makes no difference. First, the variable space only exists for it. In the variable is reserved enough space to fit the data of its type and nothing else, no matter how many times it is modified or where or how it was declared, there is only that space.

As a side note, if the variable is by reference each new assignment will likely generate a new object, otherwise it becomes meaningless, and this will swell the heap and puts pressure on GC, which is not ideal, but if you need to assign new objects you have nothing to do.

From the processing point of view also makes no difference, the variable declaration is what we call no-op. So declaring out does not bring gains because the cost of declaring in is also zero.

The variable declaration in the background occurs at the beginning of the stack frame, that is, when a scope starts every necessary space is reserved at once, no matter where it was declared. You can change the moment of the reservation, but only once. The declaration of all variables of that new scope occurs with a change of a pointer (stack Pointer), whether it is a variable or thousands of them, the cost is the same, there is only one change in the SP.

The attribution always has a cost, unless the compiler can determine that it can be deleted. And the example code has a chance that it doesn’t even occur since it’s doing the same thing and I think it’s possible to prove that it doesn’t make sense to re-allocate the same value over and over again, so that code could be written as:

{
    int x = 10;
    int y = 30;
    int z = x + y;
    while (true);
}

Actually the compiler does not:

//reservando espaço para as variáveis
.maxstack 2
.locals init (
    [0] int32
)

IL_0000: ldc.i4.s 10
IL_0002: ldc.i4.s 30
IL_0004: stloc.0
IL_0005: pop
IL_0006: br.s IL_0000

See how it looks on Sharplab. Even that generates exactly the same code.

But the Jitter does even more since nothing is used, it eliminates almost all the code leaving only the movement of stack frame and tie in the same place:

L0000: push ebp     //protege o BP
L0001: mov ebp, esp //só move o SP para o Base Pointer
L0003: jmp L0103    //fica repetindo sem parar

See on Sharplab.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.