有没有办法判断 C# 程序集是否已使用优化参数进行编译?

发布于 2024-09-15 07:15:32 字数 111 浏览 2 评论 0原文

相反,有没有办法判断它是否是在启用或禁用优化参数的情况下编译的。我不想知道它是发布版还是调试版,因为两者都可以在优化或不优化的情况下启用。从我的角度来看,尽管代码上写的是发布版本,但它真的是优化过的吗?谢谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

以可爱出名 2024-09-22 07:15:32

一种检查方法是查看 DebuggableAttribute (doc)关于程序集。如果 C# 编译器传递了 /optimize 选项,则不会设置 DisableOptimizations 标志。

注意:虽然这适用于大多数场景,但这并不是 100% 万无一失的解决方案。至少可以通过以下方式打破它

  1. 使用具有不同优化语义的另一种语言进行编译
  2. 如果用户手动定义了 DebuggableAttribute 它将优先于 C# 编译器定义的内容

One way to check is to look at the DebuggableAttribute (doc) on the assembly. The DisableOptimizations 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

  1. Compiling with another language that has different semantics for optimization
  2. If the user hand defines the DebuggableAttribute it will take precedence over what the C# compiler defines
七颜 2024-09-22 07:15:32

使用 Ildasm.exe 查看程序集清单:

  // --- The following custom attribute is added automatically, do not uncomment -------
  //  .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(
          valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) 
          = ( 01 00 02 00 00 00 00 00 ) 

这是发布版本。调试构建值是 ( 01 00 07 01 00 00 00 00 )

另一个问题是调试器可以禁用 JIT 优化器。这是 VS、工具 + 选项、调试、常规、“抑制模块加载时的 JIT 优化”中的可配置选项。如果您正在调试发布版本并想要类似的性能,那么您需要关闭它。它使调试变得更具挑战性,随着优化器重新排列和内联代码,单步执行会变得很奇怪,并且您通常无法检查局部变量的值,因为它们存储在 CPU 寄存器中。

Look at the assembly manifest with Ildasm.exe:

  // --- The following custom attribute is added automatically, do not uncomment -------
  //  .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(
          valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) 
          = ( 01 00 02 00 00 00 00 00 ) 

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.

故事↓在人 2024-09-22 07:15:32

我晚了 8 年,但如果你像我一样想要一种 C# 方式,这里是:

using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;

internal static class AssemblyExtensions
{
    public static bool IsOptimized(this Assembly asm)
    {
        var att = asm.GetCustomAttribute<DebuggableAttribute>();
        return att == null || att.IsJITOptimizerDisabled == false;
    }
}

作为扩展方法:

static void Main(string[] args)
{
    Console.WriteLine("Is optmized: " + typeof(Program).Assembly.IsOptimized());
}

I'm 8 years late but if you, like me, wanted a C# way, here it is:

using System.ComponentModel;
using System.Diagnostics;
using System.Reflection;

internal static class AssemblyExtensions
{
    public static bool IsOptimized(this Assembly asm)
    {
        var att = asm.GetCustomAttribute<DebuggableAttribute>();
        return att == null || att.IsJITOptimizerDisabled == false;
    }
}

As an extension method:

static void Main(string[] args)
{
    Console.WriteLine("Is optmized: " + typeof(Program).Assembly.IsOptimized());
}
2024-09-22 07:15:32

这篇文章可能会帮助您 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文