Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

slow performance of multidimensional array initialiser

I have some weird performance results that I cannot quite explain. It seems that this line

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

is 4 times slower than this one

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;

(and that is not even considering the fact that in this example I could leave out all those = 0 assignments)

I know that looping over a multidimensional array in c# can be slow due to the boundary checks. But there is no loop here, no boundary checks are required, and the whole array initializer line can be resolved at compile time.

The second code block however has to first initialize the array to zero, then overwrite each value individually.
So what is the problem here?

And what would be the best way to initialize this array if performance is an issue?


I used the following code to measure performance:

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);
    }
}

The results:

ArrayInitializer:       0,0007917ms
new + Array.Copy:       0,0002739ms
direct assignment:      0,0002281ms
like image 647
HugoRune Avatar asked Apr 15 '13 15:04

HugoRune


1 Answers

Here is good explanation of array initializers and why you see such different results: http://bartdesmet.net/blogs/bart/archive/2008/08/21/how-c-array-initializers-work.aspx

Basically - array initializer involves creation of custom structs, while direct assigning of each item is just direct assignment in the stack and though it faster.

like image 54
Lanorkin Avatar answered Oct 18 '22 04:10

Lanorkin