多维数组初始化器的性能降低

时间:2013-04-15 15:33:45

标签: c# arrays performance .net-4.0 multidimensional-array

我有一些奇怪的表现结果,我无法解释。 看来这一行

d = new double[4, 4]{{1, 0, 0, 0},
                     {0, 1, 0, 0},
                     {0, 0, 1, 0},
                     {0, 0, 0, 1},};

比这个慢4倍

d = new double[4, 4];
d[0, 0] = 1; d[0, 1] = 0; d[0, 2] = 0; d[0, 3] = 0; 
d[1, 0] = 0; d[1, 1] = 1; d[1, 2] = 0; d[1, 3] = 0;
d[2, 0] = 0; d[2, 1] = 0; d[2, 2] = 1; d[2, 3] = 0;
d[3, 0] = 0; d[3, 1] = 0; d[3, 2] = 0; d[3, 3] = 1;

(甚至没有考虑到在这个例子中我可以省略所有= 0作业的事实)

我知道由于边界检查,在c#中循环遍历多维数组可能会很慢。但是这里没有循环,不需要边界检查,整个数组初始化程序行可以在编译时解析。

然而,第二个代码块必须首先将数组初始化为零,然后单独覆盖每个值 那么这里的问题是什么?

如果性能有问题,那么初始化此数组的最佳方法是什么?


我使用以下代码来衡量效果:

using System;
using System.Diagnostics;
class Program
{
    public static double[,] d; // global static variable to prevent the JIT optimizing it away

    static void Main(string[] args)
    {
        Stopwatch watch;
        int numIter = 10000000; // repeat all tests this often

        double[,] d2 = new double[4, 4]{{1, 0, 0, 0},
                                        {0, 1, 0, 0},
                                        {0, 0, 1, 0},
                                        {0, 0, 0, 1},};

        // ================================================================
        // use arrayInitializer: slowest
        watch = Stopwatch.StartNew();
        for (int i = 0; i < numIter; i++)
        {
            d = new double[4, 4]{{1, 0, 0, 0},
                                {0, 1, 0, 0},
                                {0, 0, 1, 0},
                                {0, 0, 0, 1},};
        }
        Console.WriteLine("ArrayInitializer: \t{0:0.##########}ms", watch.ElapsedMilliseconds * 1.0 / numIter);

        // ================================================================
        // use Array.Copy: faster
        watch = Stopwatch.StartNew();
        for (int i = 0; i < numIter; i++)
        {
            d = new double[4, 4];
            Array.Copy(d2, d, d2.Length);
        }
        Console.WriteLine("new + Array.Copy: \t{0:0.##########}ms", watch.ElapsedMilliseconds * 1.0 / numIter);

        // ================================================================
        // direct assignment: fastest
        watch = Stopwatch.StartNew();
        for (int i = 0; i < numIter; i++)
        {
            d = new double[4, 4];
            d[0, 0] = 1; d[0, 1] = 0; d[0, 2] = 0; d[0, 3] = 0; 
            d[1, 0] = 0; d[1, 1] = 1; d[1, 2] = 0; d[1, 3] = 0;
            d[2, 0] = 0; d[2, 1] = 0; d[2, 2] = 1; d[2, 3] = 0;
            d[3, 0] = 0; d[3, 1] = 0; d[3, 2] = 0; d[3, 3] = 1;
        }
        Console.WriteLine("direct assignment: \t{0:0.##########}ms", watch.ElapsedMilliseconds * 1.0 / numIter);
    }
}

结果:

ArrayInitializer:       0,0007917ms
new + Array.Copy:       0,0002739ms
direct assignment:      0,0002281ms

1 个答案:

答案 0 :(得分:1)

以下是对数组初始值设定项的详细解释以及为什么会看到这些不同的结果:http://bartdesmet.net/blogs/bart/archive/2008/08/21/how-c-array-initializers-work.aspx

基本上 - 数组初始化程序涉及创建自定义结构,而直接分配每个项目只是在堆栈中直接赋值,虽然它更快。