【问题标题】:Is there a runtime benefit to using const local variables?使用 const 局部变量有运行时的好处吗?
【发布时间】:2010-12-15 01:03:19
【问题描述】:

除了确保它们不能被更改(根据编译器错误的调整)之外,JIT 是否对 const locals 进行任何优化?

例如。

public static int Main(string[] args)
{
    const int timesToLoop = 50;

    for (int i=0; i<timesToLoop; i++)
    {
        // ...
    }
}

【问题讨论】:

  • 运行时收益?我的直觉是它应该更有效,尽管可能微不足道。我也很好奇答案。
  • 由于这可能会导致编译时优化,我怀疑它的优化方式与全局常量相同。
  • 我意识到任何优化都是微不足道的,这更像是一个好奇的问题:)
  • 真正的好处在于维护。

标签: c# constants jit


【解决方案1】:

生成的IL不同(使用Release模式):

using constant local                   using normal local
---------------------------------------------------------------------
.entrypoint                            .entrypoint
.maxstack 2                            .maxstack 2
.locals init (                         .locals init (
    [0] int32 i)                           [0] int32 timesToLoop,
L_0000: ldc.i4.0                           [1] int32 i)
L_0001: stloc.0                        L_0000: ldc.i4.s 50 
L_0002: br.s L_0008                    L_0002: stloc.0 
L_0004: ldloc.0                        L_0003: ldc.i4.0  
L_0005: ldc.i4.1                       L_0004: stloc.1 
L_0006: add                            L_0005: br.s L_000b 
L_0007: stloc.0                        L_0007: ldloc.1 
L_0008: ldloc.0                        L_0008: ldc.i4.1 
L_0009: ldc.i4.s 50                    L_0009: add
L_000b: blt.s L_0004                   L_000a: stloc.1 
L_000d: ret                            L_000b: ldloc.1 
                                       L_000c: ldloc.0 
                                       L_000d: blt.s L_0007
                                       L_000f: ret 

如您所见,编译器将所有变量使用替换为常量的值,从而导致堆栈更小。

【讨论】:

  • 除了一个微不足道的小堆栈,它在功能上是相同的。不过很有趣。
  • @kenny 我不同意它在功能上是相同的,因为可以更改非常量的本地:P
【解决方案2】:

我使用Snippet Compiler 对代码进行了快速性能测试。我使用的代码如下:

    public static void Main()
    {
        DateTime datStart = DateTime.UtcNow;
        const int timesToLoop = 1000000;

        for (int i=0; i < timesToLoop; i++)
        {
            WL("Line Number " + i.ToString());
        }

        DateTime datEnd = DateTime.UtcNow;
        TimeSpan tsTimeTaken = datEnd - datStart;
        WL("Time Taken: " + tsTimeTaken.TotalSeconds);
        RL();
    }

注意,WL 和 RL 只是读取和写入控制台的辅助方法。

为了测试非常量版本,我只是删除了const 关键字。结果令人惊讶:

                        Time Taken (average of 3 runs)

Using const keyword         26.340s
Without const keyword       28.276s

我知道这是非常粗略的'n'ready 测试,但使用const 关键字似乎算作有效的micro-optimization

【讨论】:

  • 说到微优化,你应该使用DateTime.UtcNow而不是DateTime.Now,因为前者不需要从操作系统中查找本地时区。
【解决方案3】:

您的代码(使用 const)实际上将编译为:

public static int Main(string[] args){    
    for (int i=0; i < 50; i++)  
    {

    }
}

而变量将编译为变量:

public static int Main(string[] args){
    int timesToLoop = 50;    
    for (int i=0; i < timesToLoop; i++)  
    {

    }
}

【讨论】:

    【解决方案4】:

    这不是一个答案,只是认为分享这个会很好,但是文章没有明确提到运行时的好处:
    Coding Standard Rule #2: Use const Wherever Possible

    摘录:
    推理:尽可能使用 const 的好处是编译器强制防止意外写入应该是只读的数据。

    【讨论】:

      【解决方案5】:

      一个区别是,如果您的程序集在另一个程序集中引用了 const 字段,而您后来更改了该值,则引用程序集在重建之前仍将使用旧值。

      【讨论】:

      • 问题指的是 const 本地人,而不是 const 字段/成员。
      猜你喜欢
      • 1970-01-01
      • 2019-07-23
      • 2012-05-31
      • 2019-01-17
      • 2017-12-30
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多