Quote: " E.g. suppose I have an array of [10,10] and I have something like this (just pseudo-code to illustrate what I mean). It's intended to look at each point on the map in turn and see what the average height is of the nine-point square around that point:
For i = 0 to 9
For j=0 to 9
averageheight=0
For k=i-1 to i+1
For l=k-1 to k+1
Add averageheight, heightmap [k,l]
Next l
Next k
averageheight=averageheight/9
*do something clever with the averageheight info here*
Next j
Next i
"
I understand this is mock up code and the easy solution is to wrap the cords inside nested loops, but it's worth being aware there's a potentially mine field of overhead when that map gets bigger. Even at a tiny 10*10 field and 9 samples per point with a function on each axis, that's like 1800 function calls. You can get rid of the overhead, just depends on how big the map is going to be.
Even if you used some wrap function and got rid of the K loop and compressed the I loop into one would be a small start.
ie
For k=i-1 to i+1
For l=k-1 to k+1
Add averageheight, heightmap [ wrap(k) , warp(l) ]
Next l
Next k
could be ie
I_MinusOne = wrap(i-1)
I_PlusOne = wrap(i+1)
For l=k-1 to k+1
WrappedX = Wrap(k)
Add averageheight, heightmap [ WrappedX , l_MinusONE ]
Add averageheight, heightmap [ WrappedX , l ]
Add averageheight, heightmap [ WrappedX , l _PlusONE]
Next l
so a little more messing around, but we're doing to 3 function calls per sampling.. You can get rid of them all though, but the routine gets fatter..