Re: switching values fast





Flatman wrote On 09/16/05 01:40,:
> Hi,
>
> I wrote a program which needs to switch very fastly the contents of
> array data . I used to this as follows :
>
> for(int y=0; y<clone.height-1; y++){
> for(int x=0; x<clone.width/2-1; x++){
> int dummyvar = clone.get(x,y) ;
> clone.set(x,y,clone.get(clone.width-x,y)) ;
> clone.set(clone.width-x,y,dummyvar) ;
> }
> }
>
> Would there be a faster way for switching the data than using a dummy
> variable ?

An old computer adage states that "The fastest I/O
operation is the one you don't do," and this generalizes
to "The fastest X is the one you don't do."

Instead of reflecting each x-row, could you instead
arrange to process them "backwards" elsewhere? That is,
what are you going to do with the arrays after running
the above code? If you plan to iterate over each row
for x=0,1,2,... and get "right-to-left" instead of "left-
to-right" order, maybe you could just leave the array as
it is and iterate over x=N-1,N-2,N-3,... instead.

By the way, the end tests of both your loops look
suspicious and may be wrong. A useful way to check for
possible off-by-one errors is to consider what happens in
extreme or degenerate cases: does the code you've shown
do what you intend when clone.height==1? How about when
clone.width==2 or ==3?

--
Eric.Sosman@xxxxxxx

.