2D array vs 1D array

I read a question for the performance of a 2-dimensional array versus 1-dimensional array

But in conclusion, he says that it can be the same (depending on the cardโ€™s own card function, C does it automatically)? ...

I have a matrix with 1000 columns and 440,000,000 rows, where each element is double in C# ...

If I do some in-memory calculations, which one is better to use in terms of performance? (note that I have the memory needed to store so much information in the form of a monster) ...

+4
source share
3 answers

If what you are asking is better, a 2D array of size 1000x44000 or a 1D array of size 44000000, well, what's the difference, how much memory is going? You still have the same number of items! In the case of performance and comprehensibility, 2D is probably better. Imagine you need to manually find each column or row in a 1D array when you know exactly where they are in the 2D array.

+6
source

It depends on how many operations you perform. In the example below, I set the values โ€‹โ€‹of an array 2500 times. Array size (1000 * 1000 * 3). The 1D array took 40 seconds, and the 3D array took 1:39 minutes.

 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
source

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

You are probably better off with the version [,] (allowing the compiler (s) to determine the addressing. But the pattern of your calculations will most likely have more impact (using the locale and cache).

Also consider the option array-of-array, double[1000][] . This is a well-known โ€œfeatureโ€ of jitter that it cannot eliminate the range check in arrays [,] .

+1
source

Source: https://habr.com/ru/post/895104/


All Articles