|
Author |
Topic: if - order of conditionals/speed gains (Read 227 times) |
|
bsr
|
if - order of conditionals/speed gains
« on: Jul 28th, 2004, 1:10am » |
|
for an if statement like: if ((c[i][0] < width/2) && ((c[i][0] > width/4)) && ((c[i][1] < height/2)) && (!smp2.isPlaying())){ smp2.setPan(-0.25); smp2.play(); } would there be any speed gains at all to be had from the order in which the conditionals are written? in this example there are 3 checks on the location of an object before a check on if the sample is playing. would putting the (!smp2.isPlaying()) as the first conditional bypass the others? sorry if this is covered elsewhere or is really basic, i've not had to think about it before. cheers, jon
|
http://hippocamp.net
|
|
|
fry
|
Re: if - order of conditionals/speed gains
« Reply #1 on: Jul 28th, 2004, 2:15am » |
|
strictly speaking, if statements in java are supposed to be (i haven't tested extensively) "short circuit" operators, meaning that as soon as the condition is fulfilled, the rest of the if() is ignored. so for instance: if ((smp2 != null) && (smp2.isPlaying()) if smp2 was null, smp2 would not be checked for isPlaying(), because the condition had already been met. that said, the speedup in this case might be unnoticeable, unless isPlaying() takes a long time.
|
|
|
|
|