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)...

Community
  • 1
  • 1
edgarmtze
  • 24,683
  • 80
  • 235
  • 386
  • 3
    "Monstrous" quantity of information? It's about 330 MB; that's how much a browser uses for just a few open tabs. – Konrad Morawski Aug 13 '11 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. – Olivier Jacot-Descombes Aug 13 '11 at 15:01
  • So accesing a 1D array would be faster, I would have to do only the `*width` op right? – edgarmtze Aug 13 '11 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. – Olivier Jacot-Descombes Aug 13 '11 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. – Olivier Jacot-Descombes Aug 13 '11 at 15:19
  • I'm surprised that in this time no one has brought up that you should profile. Try both approaches, profile your code, and see what's better. The only one who can truly answer this question is *you*. We can talk about theory all day (and there are a few good points to be made) but in the end the only thing that matters is whether it makes a real difference when you implement it. – Mike Bailey Mar 04 '12 at 21:02

3 Answers3

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.

MGZero
  • 5,812
  • 5
  • 29
  • 46
6

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;
            }
         }
     }
}
bhushan
  • 71
  • 1
  • 2
  • 1
    This is the only answer providing evidence, not just an opinion! I found the explanation why 2D arrays take longer in the following blog: https://medium.com/csharp-architects/high-performance-arrays-in-c-2d55c04d37b5 TLDR: The runtime can access 1D arrays using a special IL instruction while for nD arrays it needs a method call. – Isolin Sep 21 '20 at 22:56
  • 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. – OneTrickDragon Sep 08 '21 at 13:39
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.

H H
  • 263,252
  • 30
  • 330
  • 514