How are 2D static arrays allocated?

Don nospam at nospam.com
Thu Nov 6 08:20:46 PST 2008


Lars Kyllingstad wrote:
> 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.
>>
>> Is there a general "rule" for when dynamic arrays are faster than 
>> static ones, and vice versa?
>>
>> Also, I tried multiplying large matrices both using arrays in D and 
>> using a C BLAS implementation. The BLAS algorithm is the same as the 
>> one I've written in D, and additionally includes a lot of runtime 
>> checks. Still, multiplication using BLAS was about four times faster. 
>> Is this simply because C compilers are a lot better at optimisation 
>> than the D compilers?
> 
> I just remembered that D also does bounds checking on arrays. To work 
> around this, I tried accessing the array elements through pointers. To 
> my surprise, this actually didn't affect the results very much.

My experience is that using indexing is faster in DMD. I don't know why.
To compare D and C, compile your C code with DMC. Then you'll get the 
same code generation capability. DMD and DMC are extremely weak on 
floating-point optimisation. You shouldn't see any difference between them.


More information about the Digitalmars-d-learn mailing list