How are 2D static arrays allocated?

Christopher Wright dhasenan at gmail.com
Wed Nov 5 15:41:27 PST 2008


Lars Kyllingstad wrote:
> Jarrett Billingsley wrote:
>> Performance of dynamic arrays is the same no matter where their data
>> is.  Fixed-size 2D arrays are not faster _because_ they are on the
>> stack, they just happen to be allocated on the stack.  They are faster
>> (usually) because they don't need two pointer dereferences.  You can
>> allocated a fixed-size 2D array on the heap (well.. inside a struct or
>> class anyway) and it will be just as fast.
> 
> Your "usually" interests me. I was under the impression, and it seems 
> quite logical, that static arrays are faster than dynamic ones. However, 
> recently I did some simple experiments with matrix operations 
> (multiplication, etc.), in which performance was actually a little bit 
> better for dynamic arrays.

Indexing on a static 2d array is a multiplication and and addition. 
Indexing on a dynamic one is a dereference and an addition. If the 
compiler optimizes the dereference out but not the multiplication, you 
can get cases where dynamic arrays are faster.

There are probably other cases where the efficiency differs from 
expectations, but I'm not familiar with them.


More information about the Digitalmars-d-learn mailing list