Reusing a loop iteration variable

I have seen many questions about whether variables should be declared inside or outside the scope of the loop for. This is discussed in detail, for example here , here and. The answer is that there is absolutely no difference in performance (the same IL), but for clarity, it is preferable to declare variables in the narrowest area.

I was interested to know a slightly different situation:

int i;

for (i = 0; i < 10; i++) {
    Console.WriteLine(i);
}

for (i = 0; i < 10; i++) {
    Console.WriteLine(i);
}

against

for (int i = 0; i < 10; i++) {
    Console.WriteLine(i);
}

for (int i = 0; i < 10; i++) {
    Console.WriteLine(i);
}

I expected both methods to be compiled into the same IL in Release mode. However, it is not. I will spare you with a full IL and just point out the difference. The first method has one local:

.locals init (
    [0] int32 i
)

and in the second - only two local files, one for each loop counter for:

.locals init (
    [0] int32 i,
    [1] int32 i
)

, , , .

, ?

+4
3

, , - . #, -, , , . , , , JIT , . , , , IL. JIT .

, . , JIT (x86, , x64 ), , , .

, . Visual Studio 2015 .NET 4.6.1 runtime, x86 (.. ) , JIT ( , ). inlining, . , , , "" > "Windows" > "". F5, .

            for (i = 0; i < 10; i++)
010204A2  in          al,dx  
010204A3  push        esi  
010204A4  xor         esi,esi  
            {
                Console.WriteLine(i);
010204A6  mov         ecx,esi  
010204A8  call        71686C0C  
            for (i = 0; i < 10; i++)
010204AD  inc         esi  
010204AE  cmp         esi,0Ah  
010204B1  jl          010204A6  
            }

            for (i = 0; i < 10; i++)
010204B3  xor         esi,esi  
            {
                Console.WriteLine(i);
010204B5  mov         ecx,esi  
010204B7  call        71686C0C  
            for (i = 0; i < 10; i++)
010204BC  inc         esi  
010204BD  cmp         esi,0Ah  
010204C0  jl          010204B5  
010204C2  pop         esi  
010204C3  pop         ebp  
010204C4  ret  

            for (int i = 0; i < 10; i++)
010204DA  in          al,dx  
010204DB  push        esi  
010204DC  xor         esi,esi  
            {
                Console.WriteLine(i);
010204DE  mov         ecx,esi  
010204E0  call        71686C0C  
            for (int i = 0; i < 10; i++)
010204E5  inc         esi  
010204E6  cmp         esi,0Ah  
010204E9  jl          010204DE  
            }

            for (int i = 0; i < 10; i++)
010204EB  xor         esi,esi  
            {
                Console.WriteLine(i);
010204ED  mov         ecx,esi  
010204EF  call        71686C0C  
            for (int i = 0; i < 10; i++)
010204F4  inc         esi  
010204F5  cmp         esi,0Ah  
010204F8  jl          010204ED  
010204FA  pop         esi  
010204FB  pop         ebp  
010204FC  ret  

, , .

, esi.

x64.

+9

, , , JIT.

JIT , ( ) . JIT , . , , .

, - .

IL , JIT- . , , , , . , IL .

:

  • , , .
  • , , , , .
  • , , .

JIT- # 2, # 3, IL.

+2

. # , , ( "a" + "b" ) . IL, # . , JIT.

JIT. , Release " JIT " VS

+1

Source: https://habr.com/ru/post/1675575/


All Articles