Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of 2-dimensional array vs 1-dimensional array

Tags:

arrays

c

In C, is there a difference in time and space between an m×n 2-dimensional array vs a 1-dimensional array of length m×n (for large values of m and n)? Will accessing elements be faster with a 1-dimensional array?

like image 630
vishal Avatar asked Aug 07 '09 03:08

vishal


People also ask

Why 2D array is better than 1D array?

The main difference between 1D and 2D array is that the 1D array represents multiple data items as a list while 2D array represents multiple data items as a table consisting of rows and columns. A variable is a memory location to store data of a specific type.

Is 1D better than 2D?

1D Barcoding and Scanners 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.

What is the difference between a 1 dimensional array and a 2 dimensional 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.

What is the advantage of 2D array?

With multidimensional or 2d array, it's easy to access and maintain. You don't have to use various variables for each entity which can reside in a single variable throughout your application. Every variable created takes up a specific resource which when accessed has to be looked-up.


1 Answers

In C, 2-dimensional arrays are just a neat indexing scheme for 1-dimensional arrays. Just like with a 1D array, 2D arrays allocate a single block of contiguous memory, and the A[row][col] notation is similar to saying A[row*NCOLS+col].

Usually if you were to implement your own multidimensional arrays using single dimensional arrays, you'd write an indexing function:

int getIndex(int row, int col) { return row*NCOLS+col; } 

Assuming your compiler inlines this function, the performance here would be precisely the same as if you used the built in 'indexing function' of 2D arrays.

To illustrate:

#define NROWS 10 #define NCOLS 20 

This:

int main(int argc, char *argv[]) {     int myArr[NROWS*NCOLS];     for (int i=0; i<NROWS; ++i) {        for (int j=0; j<NCOLS; ++j) {           myArr[getIndex(i,j)] = i+j;        }     }     return 0; } 

Should perform the same as this:

int main(int argc, char *argv[]) {     int myArr[NROWS][NCOLS];     for (int i=0; i<NROWS; ++i) {        for (int j=0; j<NCOLS; ++j) {           myArr[i][j] = i+j;        }     }     return 0; } 

Though as AraK pointed out, if you are jumping around rows a lot, and the rows are very large, you could hit a lot of page faults... in that case the custom indexing function (with rows and cols switched around) could help, but so could simply changing which of the dimensions in a 2-dimensional array you treat as the rows and which you treat as the columns.

like image 140
David Claridge Avatar answered Sep 21 '22 15:09

David Claridge