有没有办法判断 C# 程序集是否已使用优化参数进行编译?
相反,有没有办法判断它是否是在启用或禁用优化参数的情况下编译的。我不想知道它是发布版还是调试版,因为两者都可以在优化或不优化的情况下启用。从我的角度来看,尽管代码上写的是发布版本,但它真的是优化过的吗?谢谢。
Rather, is there a way to tell if it's been compiled with the optimization parameter enabled or disabled. I don't want to know if it's release or debug, since either can be enabled with or without optimizations. From my perspective, even though the code says it's release version, is it truly optimized? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种检查方法是查看
DebuggableAttribute
(doc)关于程序集。如果 C# 编译器传递了 /optimize 选项,则不会设置DisableOptimizations
标志。注意:虽然这适用于大多数场景,但这并不是 100% 万无一失的解决方案。至少可以通过以下方式打破它
One way to check is to look at the
DebuggableAttribute
(doc) on the assembly. TheDisableOptimizations
flag will not be set if the C# complier was passed the /optimize option.Note: Although this will work for the majority of scenarios this is not a 100% fool proof solution. It can be broken it at least the following ways
DebuggableAttribute
it will take precedence over what the C# compiler defines使用 Ildasm.exe 查看程序集清单:
这是发布版本。调试构建值是 ( 01 00 07 01 00 00 00 00 )
另一个问题是调试器可以禁用 JIT 优化器。这是 VS、工具 + 选项、调试、常规、“抑制模块加载时的 JIT 优化”中的可配置选项。如果您正在调试发布版本并想要类似的性能,那么您需要关闭它。它使调试变得更具挑战性,随着优化器重新排列和内联代码,单步执行会变得很奇怪,并且您通常无法检查局部变量的值,因为它们存储在 CPU 寄存器中。
Look at the assembly manifest with Ildasm.exe:
That's the Release version. The debug build values are ( 01 00 07 01 00 00 00 00 )
Yet another wrinkle is that the debugger can disable the JIT optimizer. That's a configurable option in VS, Tools + Options, Debugging, General, "Suppress JIT optimization on module load". You want that off if you're debugging the Release build and want comparable perf. It makes debugging more challenging, stepping acts weird as the optimizer rearranges and inlines code and you often cannot inspect the values of local variables because they are stored in CPU registers.
我晚了 8 年,但如果你像我一样想要一种 C# 方式,这里是:
作为扩展方法:
I'm 8 years late but if you, like me, wanted a C# way, here it is:
As an extension method:
这篇文章可能会帮助您 http://www.panopticoncentral.net/archive /2004/02/03/267.aspx
具体而言,在 ildasm 中,您可以查找 DebuggableAttribute 以及是否缺少 NOP。
This post might help you http://www.panopticoncentral.net/archive/2004/02/03/267.aspx
Specifically, in ildasm, you could look for the DebuggableAttribute and the absence of NOPs.