|
[Sponsors] |
June 26, 2003, 15:56 |
about multidimensional arrays in C
|
#1 |
Guest
Posts: n/a
|
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 |
|
June 26, 2003, 17:52 |
Re: about multidimensional arrays in C
|
#2 |
Guest
Posts: n/a
|
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 |
|
June 27, 2003, 02:55 |
Re: about multidimensional arrays in C
|
#3 |
Guest
Posts: n/a
|
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 |
|
June 29, 2003, 16:37 |
Re: about multidimensional arrays in C
|
#4 |
Guest
Posts: n/a
|
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 |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
multidimensional Field<> | linch | OpenFOAM Programming & Development | 0 | September 13, 2011 07:50 |
How to represent the pressure evaluated by multidimensional averaging? | renyun0511 | OpenFOAM Running, Solving & CFD | 0 | November 4, 2010 02:44 |
Define Point arrays on a curve | selim | FLUENT | 0 | June 4, 2008 10:30 |
Dynamic multidimensional array in C++? | Chen Zhi | Main CFD Forum | 13 | May 22, 2008 21:18 |
Cannot allocate memory for arrays (1400,1000) | Quarkz | Main CFD Forum | 5 | January 27, 2008 06:38 |