Is there any way to change the .NET JIT compiler to favor performance over compile time?

后端 未结 1 1831
长情又很酷
长情又很酷 2021-02-07 12:09

I was wondering if there\'s any way to change the behavior of the .NET JIT compiler, by specifying a preference for more in-depth optimizations. Failing that, it would be nice

相关标签:
1条回答
  • 2021-02-07 13:03

    This is set when you compile your assembly. There are two types of optimizations:

    • IL optimization
    • JIT Native Code quality.

    The default setting is this

     /optimize- /debug-
    

    This means unoptimized IL, and optimized native code.

     /optimize /debug(+/full/pdbonly) 
    

    This means unoptimized IL, and unoptimized native code (best debug settings).

    Finally, to get the fastest performance:

    /optimize+ /debug(-/+/full/pdbonly)
    

    This produces optimized IL and optimized native code.

    When producing unoptimized IL, the compiler will insert NOP instructions all over the code. This makes code easier to debug by allowing breakpoints to be set on control flow instructions such as for, while,if,else, try, catch etc.

    The CLR does a remarkably good job of optimizing code regardless. Once a method is JIT'ed, the pointer on a call or a callvirt instruction is pointed directly to the native code.

    Additionally, the CLR will take advantage of any architecture tricks available when JIT'ing your code. This means that an assembly ran through the JIT will run faster than an assembly pre-compiled by using Ngen (albeit with a slightly slower start up time), as NGen will compile for all platforms, and not take advantage of any tricks.

    0 讨论(0)
提交回复
热议问题