J Anderson wrote:
Greetings,
I was going through the quake2 source code (as you do) when I noticed
arrays are statically allocated on the stack instead of being allocated
dynamically. I’ve seen this before and know it’s for efficiency.
I’m basically questioning the merit of such technique nowadays, for
performance-orientated applications? Is it better to dynamically
allocate a smaller array to fit the memory or to use one huge statically
allocated array?
I don't understand your question here. The 'better' technique would be
the one that fits your needs, I suppose. If you need smaller arrays, you
would allocate smaller ones. If you need one 'huge' array, you'd
allocate a huge one.
Static allocation should be very fast (just adjusting a pointer,
usually), while dynamic allocation may be slower (locating a piece of
memory large enough, preparing it for use and eventual deallocation). It
may make sense to avoid several dynamic allocations when a single
allocation (static or dynamic) can do the job.
Or even one even larger static array and chop it up at
runtime?
Don't you think that's basically what the run-time system does to supply
you with dynamic memory? What makes you think you can do better?
Can you point me to any websites (and yes I have searched) that compare
the differences in performance using both approaches?
You could compare it yourself. I have. I found that allocating dynamic
memory was very fast, and I could not significantly improve on it.
You should keep in mind that 99% of code is *NOT* time-critical, and
most of the time you should be far more concerned with writing clean,
understandable, maintainable, bug-free code than with writing fast code.
-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.