在 Visual Studio 中哪里可以修改详细的 C# 编译器优化设置?
在Visual Studio C/C++项目中,可以很容易地在“属性页|C/C++|优化”中修改编译器的优化设置。例如,我们可能会给出不同的优化级别,例如/O2和/O3,以及高级优化,例如“省略帧指针”。
但是,我无法简单地在 Visual Studio 的 C# 项目中找到相应的 UI。我能找到的只是关闭优化:“优化代码”复选框就是我所拥有的。
C# 用户可以像 C/C++ 一样控制详细的编译器优化吗?我必须在命令行中提供编译器选项吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C# 代码的大部分优化在 JIT 编译器级别进行,而不是在 C# 编译器级别进行。基本上没有像C或C++那样详细的设置。
运行时有一些与性能相关的元素可以调整,例如 GC 策略,但不是很多。
当我从命令行构建基准测试等时,我倾向于使用类似这样的东西:(
我相信我已经看到匹配的 pdb 文件的存在会对性能产生影响,可能是在抛出异常的成本,因此
debug-
开关...但我可能是错的。)编辑:如果您想看到每一位优化所带来的差异,有一种方法可以证明很有趣:
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:
(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:
AFAIK C# 编译器没有这样详细的优化属性。优化可能已启用或禁用。
http://msdn.microsoft.com/en-us/library/6s2x2bzy。 aspx
我只发现了两个:
/filealign
指定输出文件中节的大小。/optimize
启用/禁用优化。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.有点过时,但是看这个问题的人可能会发现这很有用:
将其添加到方法签名中:
[MethodImpl(MethodImplOptions.NoOptimization)]
关闭该方法的编译器优化。
请参阅此处了解详细信息:
https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
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