Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 speed comparison
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
giuseppe105




PostPosted: Tue Oct 27, 2009 3:00 pm   Post subject: speed comparison

Is it faster to draw an image with the JLabels paint method or check an if statement?

I'm leaning towards the if statement.

even if the if statement is faster i'm still using the paint method would that change the answer?
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Oct 27, 2009 3:08 pm   Post subject: RE:speed comparison

I would assume that the if statement will execute sufficiently fast and be false sufficiently frequently that using it to skip the draw will improve speed. However...


As a thought experiment, assume that the if-execution takes F time and the image-draw takes D time. Assume that the if statement evaluates to true P portion of the time ( 0 <= P <= 1 ).

Without if statement: D execution time per draw.
With if statement: F + P * D execution time per draw.

Under which conditions would it be better to omit the if?
D < F + P * D
( 1 - P ) * D < F

At this point we can't really progress further without knowing anything about D and F, or at the very least P. One might assume that F << D, but that assumption is only valid if the if doesn't execute any extremely complicated logic.

Then there are questions about how the JVM does things, how the OS does things, whether the if would result in a page fault, what state the hardware is in, etc. Most of these are impossible to answer without profiling both kinds of execution.

TL;DR version: Profile both types and get back to us. It really depends on factors beyond our knowledge, such as the condition in your for loop, the implementation of JLabel, your computer's OS and hardware, your version of the JVM...


Final note: are you sure that's where your performance problems lie (assuming you're actually encountering performance issues)?
giuseppe105




PostPosted: Tue Oct 27, 2009 3:19 pm   Post subject: Re: speed comparison

To start off WOW thats some deep thoughts.

Yes i am sorry i could have given the code executed at least.

the normal pain method would be

.for (int a = 0; a < setnumber; a++)
.{
. for (int b = 0; b < setnumber; b++)
. {
. for (int c = 0; c < setnumber; c++)
. {
. g.drawImage
. }
. }
.}

but the if statement one would

.for (int a = 0; a < setnumber; a++)
.{
. for (int b = 0; b < setnumber; b++)
. {
. for (int c = 0; c < setnumber; c++)
. {
. if (graphic = transparent)
. {
. continue;
. }
. g.drawImage
. }
. }
.}

side question is the if statement one proper or should i put g.drawImage in the else of the if statement?
OneOffDriveByPoster




PostPosted: Tue Oct 27, 2009 11:09 pm   Post subject: Re: speed comparison

giuseppe105 @ Tue Oct 27, 2009 3:19 pm wrote:
Java:
for (int a = 0; a < setnumber; a++)
{
        for (int b = 0; b < setnumber; b++)
        {
               for (int c = 0; c < setnumber; c++)
               {
                       if (graphic = transparent)
                       {
                              continue;
                       }
                       g.drawImage
               }
         }
}

First, you have `=' and you probably mean to use `==' or `graphic.equals(transparent)'.
Second, [syntax="java"][/syntax] tags. Then, for your question.
The use of `continue' here is valid although the code here is simple enough that you can just change the condition and put the one line into the "then" part. There will be people who have varying levels of unreasonableness on where `continue', `break', the same with labels and `switch' fall-through are appropriate or not. I personally find that code if harder to read with more nested blocks and these constructs can help reduce nesting.
DemonWasp




PostPosted: Wed Oct 28, 2009 8:54 am   Post subject: RE:speed comparison

In this particular case, it's probably preferable to say "if the picture isn't transparent, draw it", but that's a minor quibble.

I would bet that it's faster to include the IF as long as it will occasionally be false (and as long as determining whether the image is transparent is fast, like checking a boolean). Evaluation of an IF is generally only a few instructions (maaaaybe 5-10) on the host machine, versus possibly thousands to draw the image. There's also the increased memory-bandwidth cost, increased cost on the graphics hardware and OS subsystems. It's a pretty safe bet.

Edit: I should also point out that you should read the contract of g.drawImage(); it may already filter transparent images as an optimisation, in which case doing this yourself is unnecessary.
giuseppe105




PostPosted: Wed Oct 28, 2009 2:05 pm   Post subject: Re: speed comparison

yes the g.drawImage has transparency its just that the way i coded the application it woudl mess the layers up if i didnt draw a blank space so skipping it is just like drawing it only saveing memory.

this question has been successfully answered.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: