Why does JIT order affect performance?

前端 未结 3 1904
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-24 05:57

Why does the order in which C# methods in .NET 4.0 are just-in-time compiled affect how quickly they execute? For example, consider two equivalent methods:

p         


        
3条回答
  •  余生分开走
    2020-12-24 06:42

    My time is 2400 and 2600 on i5-2410M 2,3Ghz 4GB ram 64bit Win 7.

    Here is my output: Single first

    After starting the process and then attaching the debugger

                SingleLineTest();
                MultiLineTest();
                SingleLineTest();
                MultiLineTest();
                SingleLineTest();
                MultiLineTest();
    --------------------------------
    SingleLineTest()
               Stopwatch stopwatch = new Stopwatch();
    00000000  push        ebp 
    00000001  mov         ebp,esp 
    00000003  push        edi 
    00000004  push        esi 
    00000005  push        ebx 
    00000006  mov         ecx,685D2C68h 
    0000000b  call        FFD91F70 
    00000010  mov         esi,eax 
    00000012  mov         ecx,esi 
    00000014  call        681D68BC 
                stopwatch.Start();
    00000019  cmp         byte ptr [esi+14h],0 
    0000001d  jne         0000002E 
    0000001f  call        681FB3E4 
    00000024  mov         dword ptr [esi+0Ch],eax 
    00000027  mov         dword ptr [esi+10h],edx 
    0000002a  mov         byte ptr [esi+14h],1 
                int count = 0;
    0000002e  xor         edi,edi 
                for (int i = 0; i < 1000000000; ++i)
    00000030  xor         edx,edx 
                {
                    count += i % 16 == 0 ? 1 : 0;
    00000032  mov         eax,edx 
    00000034  and         eax,8000000Fh 
    00000039  jns         00000040 
    0000003b  dec         eax 
    0000003c  or          eax,0FFFFFFF0h 
    0000003f  inc         eax 
    00000040  test        eax,eax 
    00000042  je          00000048 
    00000044  xor         eax,eax 
    00000046  jmp         0000004D 
    00000048  mov         eax,1 
    0000004d  add         edi,eax 
                for (int i = 0; i < 1000000000; ++i)
    0000004f  inc         edx 
    00000050  cmp         edx,3B9ACA00h 
    00000056  jl          00000032 
                }
                stopwatch.Stop();
    00000058  mov         ecx,esi 
    0000005a  call        6820F390 
                Console.WriteLine("Single-line test --> Count: {0}, Time: {1}", count, stopwatch.ElapsedMilliseconds);
    0000005f  mov         ecx,6A8B29B4h 
    00000064  call        FFD91F70 
    00000069  mov         ecx,eax 
    0000006b  mov         dword ptr [ecx+4],edi 
    0000006e  mov         ebx,ecx 
    00000070  mov         ecx,6A8AA240h 
    00000075  call        FFD91F70 
    0000007a  mov         edi,eax 
    0000007c  mov         ecx,esi 
    0000007e  call        6820ACB8 
    00000083  push        edx 
    00000084  push        eax 
    00000085  push        0 
    00000087  push        2710h 
    0000008c  call        6AFF48BC 
    00000091  mov         dword ptr [edi+4],eax 
    00000094  mov         dword ptr [edi+8],edx 
    00000097  mov         esi,edi 
    00000099  call        6A457010 
    0000009e  push        ebx 
    0000009f  push        esi 
    000000a0  mov         ecx,eax 
    000000a2  mov         edx,dword ptr ds:[039F2030h] 
    000000a8  mov         eax,dword ptr [ecx] 
    000000aa  mov         eax,dword ptr [eax+3Ch] 
    000000ad  call        dword ptr [eax+1Ch] 
    000000b0  pop         ebx 
            }
    000000b1  pop         esi 
    000000b2  pop         edi 
    000000b3  pop         ebp 
    000000b4  ret 
    

    Multi first:

                MultiLineTest();
    
                SingleLineTest();
                MultiLineTest();
                SingleLineTest();
                MultiLineTest();
                SingleLineTest();
                MultiLineTest();
    --------------------------------
    SingleLineTest()
                Stopwatch stopwatch = new Stopwatch();
    00000000  push        ebp 
    00000001  mov         ebp,esp 
    00000003  push        edi 
    00000004  push        esi 
    00000005  push        ebx 
    00000006  mov         ecx,685D2C68h 
    0000000b  call        FFF31EA0 
    00000010  mov         esi,eax 
    00000012  mov         dword ptr [esi+4],0 
    00000019  mov         dword ptr [esi+8],0 
    00000020  mov         byte ptr [esi+14h],0 
    00000024  mov         dword ptr [esi+0Ch],0 
    0000002b  mov         dword ptr [esi+10h],0 
                stopwatch.Start();
    00000032  cmp         byte ptr [esi+14h],0 
    00000036  jne         00000047 
    00000038  call        682AB314 
    0000003d  mov         dword ptr [esi+0Ch],eax 
    00000040  mov         dword ptr [esi+10h],edx 
    00000043  mov         byte ptr [esi+14h],1 
                int count = 0;
    00000047  xor         edi,edi 
                for (int i = 0; i < 1000000000; ++i)
    00000049  xor         edx,edx 
                {
                    count += i % 16 == 0 ? 1 : 0;
    0000004b  mov         eax,edx 
    0000004d  and         eax,8000000Fh 
    00000052  jns         00000059 
    00000054  dec         eax 
    00000055  or          eax,0FFFFFFF0h 
    00000058  inc         eax 
    00000059  test        eax,eax 
    0000005b  je          00000061 
    0000005d  xor         eax,eax 
    0000005f  jmp         00000066 
    00000061  mov         eax,1 
    00000066  add         edi,eax 
                for (int i = 0; i < 1000000000; ++i)
    00000068  inc         edx 
    00000069  cmp         edx,3B9ACA00h 
    0000006f  jl          0000004B 
                }
                stopwatch.Stop();
    00000071  mov         ecx,esi 
    00000073  call        682BF2C0 
                Console.WriteLine("Single-line test --> Count: {0}, Time: {1}", count, stopwatch.ElapsedMilliseconds);
    00000078  mov         ecx,6A8B29B4h 
    0000007d  call        FFF31EA0 
    00000082  mov         ecx,eax 
    00000084  mov         dword ptr [ecx+4],edi 
    00000087  mov         ebx,ecx 
    00000089  mov         ecx,6A8AA240h 
    0000008e  call        FFF31EA0 
    00000093  mov         edi,eax 
    00000095  mov         ecx,esi 
    00000097  call        682BABE8 
    0000009c  push        edx 
    0000009d  push        eax 
    0000009e  push        0 
    000000a0  push        2710h 
    000000a5  call        6B0A47EC 
    000000aa  mov         dword ptr [edi+4],eax 
    000000ad  mov         dword ptr [edi+8],edx 
    000000b0  mov         esi,edi 
    000000b2  call        6A506F40 
    000000b7  push        ebx 
    000000b8  push        esi 
    000000b9  mov         ecx,eax 
    000000bb  mov         edx,dword ptr ds:[038E2034h] 
    000000c1  mov         eax,dword ptr [ecx] 
    000000c3  mov         eax,dword ptr [eax+3Ch] 
    000000c6  call        dword ptr [eax+1Ch] 
    000000c9  pop         ebx 
            }
    000000ca  pop         esi 
    000000cb  pop         edi 
    000000cc  pop         ebp 
    000000cd  ret
    

提交回复
热议问题