6

I have read the question for Performance of 2-dimensional array vs 1-dimensional array

But in conclusion it says could be the same (depending the map own map function, C does this automatically)?...

I have a matrix wich has 1,000 columns and 440,000,000 rows where each element is a double in C#...

If I am doing some computations in memory, which one could be better to use in performance aspect? (note that I have the memory needed to hold such a monstruos quantity of information)...

6
  • 3
    "Monstrous" quantity of information? It's about 330 MB; that's how much a browser uses for just a few open tabs. Commented Aug 13, 2011 at 14:49
  • Of cause acessing a 1D-array is faster than a 2D-array, but if you need a 2D array, then simulating it using a 1D-array requires you to make the calculations manually, that otherwise happen automatically, and those will probably have about the same time penalty. Commented Aug 13, 2011 at 15:01
  • So accesing a 1D array would be faster, I would have to do only the *width op right? Commented Aug 13, 2011 at 15:09
  • 1
    As a general programming rule, it is always better to aim for a simple, comprehensible and logical solution. Optimizations tend to lead to bad and complicated solutions. This does not mean, that you are not allowed to optimize, however you should not do it right from the start. Do this later, if you find that your code is too slow or too memory hungry. If your solution is well structured, then it should be easy to implement changes later. Commented Aug 13, 2011 at 15:12
  • The 1D array is only faster, if you really use it as a 1D array and do not have to make index calculations (the width calculations) in order to use it like a 2D array. These are exactly these calculations which make it slower. Commented Aug 13, 2011 at 15:19

3 Answers 3

7

If what you're asking is which is better, a 2D array of size 1000x44000 or a 1D array of size 44000000, well what's the difference as far as memory goes? You still have the same number of elements! In the case of performance and understandability, the 2D is probably better. Imagine having to manually find each column or row in a 1D array, when you know exactly where they are in a 2D array.

Sign up to request clarification or add additional context in comments.

Comments

7

It depends on how many operations you are performing. In the below example, I'm setting the values of the array 2500 times. Size of the array is (1000 * 1000 * 3). The 1D array took 40 seconds and the 3D array took 1:39 mins.

var startTime = DateTime.Now;
Test1D(new byte[1000 * 1000 * 3]);
Console.WriteLine("Total Time taken 1d = " + (DateTime.Now - startTime));

startTime = DateTime.Now;
Test3D(new byte[1000,1000,3], 1000, 1000);
Console.WriteLine("Total Time taken 3D = " + (DateTime.Now - startTime));

public static void Test1D(byte[] array)
{
    for (int c = 0; c < 2500; c++)
    {
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = 10;
        }
    }
}

public static void Test3D(byte[,,] array, int w, int h)
{
    for (int c = 0; c < 2500; c++)
    {
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                array[i, j, 0] = 10;
                array[i, j, 1] = 10;
                array[i, j, 2] = 10;
            }
         }
     }
}

2 Comments

This is the only answer providing evidence, not just an opinion! I found the explanation why 2D arrays take longer in the following blog: medium.com/csharp-architects/… TLDR: The runtime can access 1D arrays using a special IL instruction while for nD arrays it needs a method call.
This is indeed helpful because of th emperical evidence. But when you want to do more to your arrays than simply assign values to them but want to maipulate certain values you have to perform index calculations. So for a "fairer" comparison test you should not simply use i in your loop but also two loops and the position calculation as would have been done when you want to set a specific value at point X,Y,Z. I have done this and manually calculating indecies took 14.3s and with the 2D array it took 11.8s.
1

The difference between double[1000,44000] and double[44000000] will not be significant.

You're probably better of with the [,] version (letting the compiler(s) figure out the addressing. But the pattern of your calculations is likely to have more impact (locality and cache use).

Also consider the array-of-array variant, double[1000][]. It is a known 'feature' of the Jitter that it cannot eliminate range-checking in the [,] arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.