Is there a way to see the native code produced by theJITter for given C# / CIL?

前端 未结 3 421
时光取名叫无心
时光取名叫无心 2020-12-08 15:23

In a comment on this answer (which suggests using bit-shift operators over integer multiplication / division, for performance), I queried whether this would actually be fast

3条回答
  •  隐瞒了意图╮
    2020-12-08 16:11

    You won't get meaningful results until you configure the debugger. Tools + Options, Debugging, General, turn off "Suppress JIT optimization on module load". Switch to the Release mode configuration. A sample snippet:

    static void Main(string[] args) {
      int value = 4;
      int result = divideby2(value);
    }
    

    You are doing it right if the disassembly looks like this:

    00000000  ret  
    

    You'll have to fool the JIT optimizer to force the expression to be evaluated. Using Console.WriteLine(variable) can help. Then you ought to see something like this:

    0000000a  mov         edx,2 
    0000000f  mov         eax,dword ptr [ecx] 
    00000011  call        dword ptr [eax+000000BCh] 
    

    Yup, it evaluated the result at compile time. Works pretty well, doesn't it.

提交回复
热议问题