Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2D array vs 1D array

Tags:

arrays

c#

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

like image 224
edgarmtze Avatar asked Aug 13 '11 14:08

edgarmtze


People also ask

What is difference between 1D and 2D array?

A one-dimensional array stores a single list of various elements having a similar data type. A two-dimensional array stores an array of various arrays, or a list of various lists, or an array of various one-dimensional arrays. It represents multiple data items in the form of a list.

Is 1D array better than 2D array?

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.

Is a 1D array faster than a 2D array?

Speed: The 1D array may be faster than the 2D array because the memory for the 2D array would not be contiguous, so cache misses would become a problem.

Is 1D better than 2D?

1D barcode scanners can only scan 1D barcodes. Their range, however, is 50% greater than a 2D imager, and they have better motion tolerance. This makes them a good choice if your staff will need to scan items from a distance or while moving along in a cart. They are also the more economical choice of the two.


2 Answers

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.

like image 58
MGZero Avatar answered Sep 22 '22 23:09

MGZero


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;
            }
         }
     }
}
like image 25
bhushan Avatar answered Sep 26 '22 23:09

bhushan