Jimmy Johnson wrote:
> Is there a better way than the for loop to initialize this 2d array?
> 
>       buffer = new double* [width];
>       for (i = 0; i < width; i++) {
>               buffer [i] = new double [height];
>       };
>               // Set z-buffer to background
>       for (i = 0; i < width; i++) {
>               for (j = 0; j < height; j++) {
>                       buffer[i][j] = -10000;  // smallest();
>               };
>       };
> 
> It seems to be pretty slow.
> 
> Thanks, 
> 
> Jimmy J. Johnson

Yes.  One (or two) allocation(s) instead of many smaller ones.  Since 
you know exactly how many elements you need, a single allocation can be 
done.  Even if you did NOT know how many elements to allocate, it is 
almost always faster to precalculate how many elements, allocate the 
amount of memory needed, and then perform the actual manipulation of the 
memory.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to