gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > If/Else

#43413 - Ultima2876 - Mon May 23, 2005 5:42 am

How fast are these if/else conditionals? As in, anyone know how many processor cycles they use? Or do you need more information, like specifically what the if contains?

Something like:

Code:
if(temp >= cold)
{
  //whatever
 }
else { //whatever }


or

Code:
if(temp >= 75)
{
  //whatever
 }
else { //whatever }


If I'm doing a lot of these (a few hundred) would it be wasting a lot of cycles, or are they just single cycle or two-cycle operations?

Thanks.

#43415 - poslundc - Mon May 23, 2005 6:14 am

There is no simple answer to your question. The number of cycles depends on the processor mode, what region of memory the code is running from, etc.

And yes, the contents of the conditional affect execution time, because any code inside the conditional must be executed, whether it's a simple comparison of two variables (like your example), a function call, or whatever else you put in there.

The bigger question isn't the number you're doing, but how often you're doing them. A few hundred isn't necessarily a big deal if it's only happening once a frame. But it would still be wise to nest your if-statements so that you aren't doing comparisons when they aren't necessary, and use switch statements where they are appropriate (which can be compiled into jump tables to save on comparisons).

Keep in mind the Pareto rule: 80% of the processor's time is usually spent within 20% of the code - usually inside nested loops somewhere. Find that 20% of code and optimize it; don't worry so much about the general case.

Dan.

#43436 - jma - Mon May 23, 2005 3:19 pm

poslundc wrote:
There is no simple answer to your question. The number of cycles depends on the processor mode, what region of memory the code is running from, etc.


Another one most people forget about (which I think Dan was touching on with processor mode) is the size of the conditional; this is one of the greatest poster-boys for factoring. In thumb mode, the largest branch instruction (conditional) is 8-bit (signed), so ~127 bytes. So let's take a simple example:

Code:
if (temp >= 75) {
  // do gobs of code that compile to > 128 bytes
} else {
  // do gobs more code
}


Now the conditional will have to compile to more than just a simple compare/branch, because a branch will be too far. Most likely it will compile to a compare/not-branch/branch-with-link (or something similar). Note that the compiler you use could impact this as well. If your else section was < 128 bytes, it might put that first and switch the test to < instead of >=.

Now,

Ultima2876 wrote:
If I'm doing a lot of these (a few hundred) would it be wasting a lot of cycles, or are they just single cycle or two-cycle operations?


In your case, assuming everything else is good (thumb in ROM or arm in IWRAM, and the code inside the conditional is of a reasonable size), then you are talking about a couple cycles. The best way for you to improve your performance if you are doing this in a loop, and temp isn't changing would be to either a) precalculate the boolean or b) mark temp as going into a register (which it probably already is).

But, please.... profile a finished gob of code first and find out where your slow-down is. I can't count the number of programmers I've seen try and optimize (read: waste time on) a section of code that just doesn't get hit that often, and isn't really the bottleneck.

Best wishes,
Jeff M.
_________________
massung@gmail.com
http://www.retrobyte.org

#43444 - Ultima2876 - Mon May 23, 2005 5:05 pm

Thanks very much, both you guys, you've helped a lot ^_^