|
|
On Tue, 01 Apr 2008 11:08:34 +0200, Warp <war### [at] tagpovrayorg> wrote:
> Fredrik Eriksson <fe79}--at--{yahoo}--dot--{com> wrote:
>> That stack allocation is fast should come as no surprise since it
>> typically requires a grand total of zero additional CPU instructions. In
>> the worst case it requires a single one-cycle instruction.
>
> But so does using a pre-allocated array. The array is *already*
> allocated.
> The function doesn't need to allocate it each time. Yet, in some cases,
> the function becomes faster by using the stack-allocated array than the
> pre-allocated array in the heap.
Two points to consider:
- Data locality
Allocating the array from the stack keeps it close to the local variables,
thus confining memory accesses to a smaller area. This plays better with
memory caches.
- Register contention
Accessing a heap-allocated array requires loading its address into a CPU
register. Accessing a stack-allocated array can be done with the stack
register, freeing other registers for use in calculations.
--
FE
Post a reply to this message
|
|