CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   Main CFD Forum (http://www.cfd-online.com/Forums/main/)
-   -   about multidimensional arrays in C (http://www.cfd-online.com/Forums/main/6292-about-multidimensional-arrays-c.html)

Adriana June 26, 2003 15:56

about multidimensional arrays in C
 
Hello.

what is more efficient (time memory) for 2D or 3D arrays in C ?

a) float ***A; A = ()calloc(...); ... A[layer][row][col] = ...;

or

b) float *B; B = ()calloc(...); ... B[col + Ncols*row + Ncols*Nrows*layer] = ...;

Thanks

Ravi June 26, 2003 17:52

Re: about multidimensional arrays in C
 
Hi

I would choose b. I have used both before and figured b is more eficient than a. Probably, b implies, we have contigous memory locations and hence easy to access.

Obviously, for ease of implementation a is better and you can comprehend it easily when you look at your code at a later date.

Ravi

jdc June 27, 2003 02:55

Re: about multidimensional arrays in C
 
Hi,

I agree with Ravi about choosing b for efficiency.

To really account for efficiency, you need to loop on the first index, eg:

for (int ic=0; ic<NumberOfColumns; ic++) { for (int ir=0; ir<NumberOfRows; ir++) {

ind=ir+ic*NumberOfRows

B(ind) } }

The ideal being:

for (int ind=0; ind<NumberOfColumns*NumberOfRows; ind++) { B(ind) }

which implied that B is dimension to your problem.

Julien

Adriana June 29, 2003 16:37

Re: about multidimensional arrays in C
 
thanks Ravi and Julien.

I have another question. If the option b) is used many times inside a loop, the two products and three additions for the index can reduce the speed ?

Adriana


All times are GMT -4. The time now is 16:04.