Where can I modify detailed C# compiler optimization settings in Visual Studio?

本小妞迷上赌 提交于 2019-12-07 15:20:42

问题


In Visual Studio C/C++ projects, it's easy to modify compiler's optimization settings in "Property Pages | C/C++ | Optimization". For example, we may give different optimization levels such as /O2 and /O3, as well as advanced optimizations like "Omit Frame Pointers".

However, I can't simply find corresponding UIs in C# project of Visual Studio. All I can find is just turning off optimizations: the "Optimize code" check box is all I've got.

Can C# users control detailed compiler's optimizations like C/C++? Do I have to give compiler options in command line?


回答1:


Much of the optimisation of C# code goes on at the JIT compiler level, rather than the C# compiler. Basically there are no such detailed settings as the ones available in C or C++.

There are a few performance-related elements of the runtime that can be tweaked, such as GC strategies, but not a great deal.

When I'm building benchmark tests etc from the command line I tend to just use something like this:

csc /o+ /debug- Test.cs

(I believe I have seen the presence of a matching pdb file make a difference to performance, possibly in terms of the cost of exceptions being thrown, hence the debug- switch... but I could be wrong.)

EDIT: If you want to see the difference each bit of optimization makes, there's one approach which could prove interesting:

  • Compile the same code with and without optimization
  • Use ildasm or Reflector in IL mode to see what the differences are
  • Apply the same changes one at a time manually (using ilasm) and measure how much each one has



回答2:


AFAIK C# compiler has no such detailed optimization properties. Probably optimization is either enabled or disabled.


http://msdn.microsoft.com/en-us/library/6s2x2bzy.aspx

I found just two:

  • /filealign Specifies the size of sections in the output file.

  • /optimize Enables/disables optimizations.




回答3:


A bit OT, but someone looking at this question might find this useful:

Adding this to method signature: [MethodImpl(MethodImplOptions.NoOptimization)]

turns off compiler optimizations for that method.

See here for details: https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396



来源:https://stackoverflow.com/questions/3226157/where-can-i-modify-detailed-c-sharp-compiler-optimization-settings-in-visual-stu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!