数组边界检查for循环中的优化

时间:2013-03-05 09:42:52

标签: c# arrays for-loop compiler-optimization bounds-check-elimination

        var ar = new int[500000000];

        var sw = new Stopwatch();
        sw.Start();

        var length = ar.Length;
        for (var i = 0; i < length; i++)
        {
            if (ar[i] == 0);
        }

        sw.Stop();

sw.ElapsedMilliseconds:~2930ms

        var ar = new int[500000000];

        var sw = new Stopwatch();
        sw.Start();

        for (var i = 0; i < ar.Length; i++)
        {
            if (ar[i] == 0);
        }

        sw.Stop();

sw.ElapsedMilliseconds:~3520ms

Win8x64,VS12,.NET4.5,发布版本,“优化代码”。

据我所知,第二种方法应该更快,因为数组边界检查优化。我错过了什么吗?

4 个答案:

答案 0 :(得分:6)

我也在调试器之外使用Win8 x64,.NET 4.5,Release版本(这是一个重要的版本);我明白了:

0: 813ms vs 421ms
1: 439ms vs 420ms
2: 440ms vs 420ms
3: 431ms vs 429ms
4: 433ms vs 427ms
5: 424ms vs 437ms
6: 427ms vs 434ms
7: 430ms vs 432ms
8: 432ms vs 435ms
9: 430ms vs 430ms
10: 427ms vs 418ms
11: 422ms vs 421ms
12: 434ms vs 420ms
13: 439ms vs 425ms
14: 426ms vs 429ms
15: 426ms vs 426ms
16: 417ms vs 432ms
17: 442ms vs 425ms
18: 420ms vs 429ms
19: 420ms vs 422ms

第一个支付JIT /“融合”成本,但总体而言大致相同(每列中的一些看起来更快,但总体上说不多)。

using System;
using System.Diagnostics;
static class Program
{
    static void Main()
    {
        var ar = new int[500000000];

        for (int j = 0; j < 20; j++)
        {
            var sw = Stopwatch.StartNew();
            var length = ar.Length;
            for (var i = 0; i < length; i++)
            {
                if (ar[i] == 0) ;
            }

            sw.Stop();
            long hoisted = sw.ElapsedMilliseconds;

            sw = Stopwatch.StartNew();
            for (var i = 0; i < ar.Length; i++)
            {
                if (ar[i] == 0) ;
            }
            sw.Stop();
            long direct = sw.ElapsedMilliseconds;

            Console.WriteLine("{0}: {1}ms vs {2}ms", j, hoisted, direct);
        }
    }
}

答案 1 :(得分:5)

我对此进行了更多调查,发现很难制作一个实际显示边界检查消除优化效果的基准。

旧基准的第一个问题:

  • 反汇编显示JIT编译器能够优化第一个版本。这对我来说是一个惊喜,但拆卸不是谎言。当然,这完全违背了这个基准的目的。 修复:将长度作为函数参数。
  • 数组太大,这意味着缓存未命中,这会给我们的信号增加很多噪音。 修复:使用短数组但多次循环。

但现在真正的问题是:它正在做一些过于聪明的事情。内循环中没有数组边界测试,即使循环的长度来自函数参数。生成的代码不同,但内部循环基本相同。不完全(不同的寄存器等)但它遵循相同的模式:

_loop: mov eax, [somewhere + index]
       add index, 4
       cmp index, end
       jl _loop

执行时间没有显着差异,因为生成的代码中最重要的部分没有显着差异。

答案 2 :(得分:1)

我认为答案是垃圾收集器正在运行并改变你的时间。

免责声明:我无法看到OP代码的整个上下文,因为您没有发布可编译的示例;我假设你正在重新分配数组而不是重用它。如果没有,那么这不是正确答案!

考虑以下代码:

using System;
using System.Diagnostics;

namespace Demo
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var ar = new int[500000000];
            test1(ar);
            //ar = new int[500000000]; // Uncomment this line.
            test2(ar);
        }

        private static void test1(int[] ar)
        {
            var sw = new Stopwatch();
            sw.Start();

            var length = ar.Length;
            for (var i = 0; i < length; i++)
            {
                if (ar[i] == 0);
            }

            sw.Stop();                
            Console.WriteLine("test1 took " + sw.Elapsed);
        }

        private static void test2(int[] ar)
        {
            var sw = new Stopwatch();
            sw.Start();

            for (var i = 0; i < ar.Length; i++)
            {
                if (ar[i] == 0);
            }

            sw.Stop();
            Console.WriteLine("test2 took " + sw.Elapsed);
        }
    }
}

在我的系统上打印:

test1 took 00:00:00.6643788
test2 took 00:00:00.3516378

如果我取消注释标记为// Uncomment this line.的行,则时间将更改为:

test1 took 00:00:00.6615819
test2 took 00:00:00.6806489

这是因为GC收集了前一个数组。

[编辑]为了避免JIT启动成本,我将整个测试放入循环中:

for (int i = 0; i < 8; ++i)
{
    test1(ar);
    ar = new int[500000000]; // Uncomment this line.
    test2(ar);
}

然后第二个数组分配注释掉的结果是:

test1 took 00:00:00.6437912
test2 took 00:00:00.3534027
test1 took 00:00:00.3401437
test2 took 00:00:00.3486296
test1 took 00:00:00.3470775
test2 took 00:00:00.3675475
test1 took 00:00:00.3501221
test2 took 00:00:00.3549338
test1 took 00:00:00.3427057
test2 took 00:00:00.3574063
test1 took 00:00:00.3566458
test2 took 00:00:00.3462722
test1 took 00:00:00.3430952
test2 took 00:00:00.3464017
test1 took 00:00:00.3449196
test2 took 00:00:00.3438316

启用第二个阵列分配:

test1 took 00:00:00.6572665
test2 took 00:00:00.6565778
test1 took 00:00:00.3576911
test2 took 00:00:00.6910897
test1 took 00:00:00.3464013
test2 took 00:00:00.6638542
test1 took 00:00:00.3548638
test2 took 00:00:00.6897472
test1 took 00:00:00.4464020
test2 took 00:00:00.7739877
test1 took 00:00:00.3835624
test2 took 00:00:00.8432918
test1 took 00:00:00.3496910
test2 took 00:00:00.6471341
test1 took 00:00:00.3486505
test2 took 00:00:00.6527160

请注意,由于GC,test2始终需要更长的时间。

不幸的是,GC使得计时结果毫无意义。

例如,如果我将测试代码更改为:

for (int i = 0; i < 8; ++i)
{
    var ar = new int[500000000];
    GC.Collect();
    test1(ar);
    //ar = new int[500000000]; // Uncomment this line.
    test2(ar);
}

随着该行被注释掉,我得到:

test1 took 00:00:00.6354278
test2 took 00:00:00.3464486
test1 took 00:00:00.6672933
test2 took 00:00:00.3413958
test1 took 00:00:00.6724916
test2 took 00:00:00.3530412
test1 took 00:00:00.6606178
test2 took 00:00:00.3413083
test1 took 00:00:00.6439316
test2 took 00:00:00.3404499
test1 took 00:00:00.6559153
test2 took 00:00:00.3413563
test1 took 00:00:00.6955377
test2 took 00:00:00.3364670
test1 took 00:00:00.6580798
test2 took 00:00:00.3378203

并且没有注释:

test1 took 00:00:00.6340203
test2 took 00:00:00.6276153
test1 took 00:00:00.6813719
test2 took 00:00:00.6264782
test1 took 00:00:00.6927222
test2 took 00:00:00.6269447
test1 took 00:00:00.7010559
test2 took 00:00:00.6262000
test1 took 00:00:00.6975080
test2 took 00:00:00.6457846
test1 took 00:00:00.6796235
test2 took 00:00:00.6341214
test1 took 00:00:00.6823508
test2 took 00:00:00.6455403
test1 took 00:00:00.6856985
test2 took 00:00:00.6430923

我认为这个测试的道德是:这个特定测试的GC与其他代码相比是一个巨大的开销,它完全扭曲了时序结果,并且它们不能被置信任何意义。

答案 3 :(得分:0)

你在第二个上调用一个属性,所以它会慢ar.Length