Multi dimensional array question.

Jonathan M Davis jmdavisprog at gmail.com
Thu Jul 15 22:40:03 PDT 2010


On Thursday 15 July 2010 22:20:17 bearophile wrote:
> Jonathan M Davis:
> > Personally, I'd advise you to just use dynamic arrays unless you do
> > some profiling and find that a static array is better in a particular
> > case.
> 
> To program you need a less naive view. I sometimes start using dynamic
> arrays everywhere because they are handy, then I profile code and then
> replace some critical allocations of dynamic arrays with static arrays,
> and I usually see good speed-ups. With some brain you can often find a
> static size (or a way to compute it statically) that is good for the
> algorithm/problem you are working on. In such cases using a static array
> is often better, the GC doesn't need to deallocate it, and heap activity
> is one of the most important causes of low performance in programs (or if
> they are allocated inside class/struct instances you remove some heap
> allocations, coalescing them). Static arrays are simpler, so in some
> situations they can make the code simpler.
> 
> Bye,
> bearophile


Well, like I said: Use dynamic arrays unless profiling shows that static arrays 
would be better in a particular situation. If you really expect a section of 
code to need it, then it makes sense to use a static array up front. But for the 
most part, dynamic arrays should be fine, and personally, I find them easier to 
use since you have less to worry about when dealing with them. However, it's 
certainly true that sections critical to efficiency may require static arrays, and 
there's nothing wrong with choosing static arrays in either case. But I see no 
reason to use static arrays normally unless profiling shows that it would be of 
benefit. Most of the programs that I do for my own use probably wouldn't care 
either way. If I were using D at work (efficiency matters a lot more with what I 
do there), it might be different. But regardless, I wouldn't generally use static 
arrays unless it were clear that they were necessary.

If anything though, I'm likely to be switch to use Array more now that we have 
it.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list