Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2005

Tip: Looking for answers? Try searching our database.

switching values fast

Thread view: 
Flatman - 16 Sep 2005 06:40 GMT
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 ?

Signature

Erik

Roedy Green - 16 Sep 2005 07:25 GMT
>Would there be a faster way for switching the data than using a dummy
>variable ?

what is the code for clone.get and clone.set?
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Flatman - 16 Sep 2005 08:05 GMT
| what is the code for clone.get and clone.set?

My app is based on processing.org . Those get and set methods are from
their library and I am not able to modify it :-/
clone is an instance of the PImage class which is based on the more
general Image class I think ...

Signature

Erik

Roedy Green - 16 Sep 2005 08:48 GMT
>My app is based on processing.org . Those get and set methods are from
>their library and I am not able to modify it :-/
>clone is an instance of the PImage class which is based on the more
>general Image class I think ...

What I was thinking was moving this method to a subset of the PImage
class and effectively inlining the getter/setters and looking for
optimisations in the inlined code.

You also might big around in their library to see if you can get the
same effect combining some of their other methods.   Chances are that
will be faster than element by element processing.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Remi Arntzen - 16 Sep 2005 07:36 GMT
When you mentioned copying arrays I first thought
System.arraycopy(Object src, int srcPos, Object dest, int destPos, int
length)
Eric Sosman - 19 Sep 2005 20:27 GMT
Flatman wrote On 09/16/05 01:40,:
> Hi,
>
[quoted text clipped - 11 lines]
> 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?

Signature

Eric.Sosman@sun.com

Flatman - 20 Sep 2005 10:46 GMT
|     Instead of reflecting each x-row, could you instead
| arrange to process them "backwards" elsewhere?  That is,
[quoted text clipped - 3 lines]
| to-right" order, maybe you could just leave the array as
| it is and iterate over x=N-1,N-2,N-3,... instead.

the reason I do this is that the clone is an image to be displayed on
screen , but it has to be the mirror of the original one

|     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?

The code works nice . height has to be at least 768 (checked before in
code) .

Thanks

Signature

Erik

Eric Sosman - 20 Sep 2005 16:05 GMT
Flatman wrote On 09/20/05 05:46,:

> |     By the way, the end tests of both your loops look
> | suspicious and may be wrong.  A useful way to check for
[quoted text clipped - 5 lines]
> The code works nice . height has to be at least 768 (checked before in
> code) .

   That wasn't the point I was trying to make.  For
reference, here's your original loop:

>    for(int y=0; y<clone.height-1; y++){
>        for(int x=0; x<clone.width/2-1; x++){
[quoted text clipped - 3 lines]
>        }
>    }

   Now: If clone.height is the total number of rows in
your image, observe that the bottommost row never gets
processed; the loop stops after processing the next-to-
last row.  The point of imagining a one-row image is to
make this fact stand out: If clone.height is 1 the loop
begins by testing whether 0 < 1-1, and since this test
yields false, the loop never executes.  Is it really your
intention to leave the last row un-reflected, or have you
fallen victim to an off-by-one error?

   Similarly with the inner loop.  If clone.width is the
number of pixels in each row, then imagine what happens with
a two-column row: the loop tests whether 0 < 2/2-1, the test
fails, and the loop doesn't execute.  The point is not that
you really intend to process images of just two columns, but
that the misbehavior on a two-column image shows that the loop
is set up incorrectly and will also misbehave on images of
realistic width.

   All this is, of course, based on the supposition that
clone.width and clone.height are the dimensions of the
image you're trying to reflect.  The arithmetic in the two
clone.set() calls suggests that's the case, but it isn't
absolutely certain: maybe you've got an extra "guard" row
or column that resides in clone but isn't considered part
of the image, or is to be left alone for some reason.  It
doesn't seem likely, but it's possible -- which is why I
said that your loops "may be" wrong, not that they "are"
wrong.  You decide.

Signature

Eric.Sosman@sun.com

Flatman - 21 Sep 2005 07:08 GMT
| yields false, the loop never executes.  Is it really your
| intention to leave the last row un-reflected, or have you
| fallen victim to an off-by-one error?

You're right, I'm missing a row . Since the Jpeg is black on that row I
didn't notice it . Thanks, bug corrected
Signature

Erik



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.