Ferrous Moon http://www.ferrousmoon.com:80/forums/ |
|
Nesting vs Chaining http://www.ferrousmoon.com:80/forums/viewtopic.php?f=45&t=2107 |
Page 1 of 1 |
Author: | eddieringle [Thu Dec 30, 2010 2:55 pm ] |
Post subject: | Nesting vs Chaining |
I'm currently in the process of developing a game for Android, and while writing a method to check if a touch event occurred inside the bounds of a shape, something occurred to me. Given that my current code looks like this: Code: public boolean isTouchingShape(final float touchX, final float touchY, final Shape shape) {
final float x = shape.getX();
final float y = shape.getY();
final float w = shape.getWidthScaled();
final float h = shape.getHeightScaled();
if (touchX > x && touchX < x + w) {
if (touchY > y && touchY < y + h) {
return true;
}
}
return false;
}
Is it more efficient to nest conditionals like I have done above or is it better to chain conditions into one expression? For example:
Code: public boolean isTouchingShape(final float touchX, final float touchY, final Shape shape) {
final float x = shape.getX();
final float y = shape.getY();
final float w = shape.getWidthScaled();
final float h = shape.getHeightScaled();
if ((touchX > x && touchX < x + w) && (touchY > y && touchY < y +h)) {
return true;
}
return false;
}
|
Author: | prophile [Sat Jan 01, 2011 11:54 am ] |
Post subject: | Re: Nesting vs Chaining |
No, nor is it less efficient. The generated bytecode will be identical for both cases - four tests followed by conditional jumps. EDIT: if you want to make it more efficient, make the function final. This will avoid the overhead of a virtual method dispatch whenever it's called. |
Author: | eddieringle [Sat Jan 01, 2011 12:51 pm ] |
Post subject: | Re: Nesting vs Chaining |
Quote: No, nor is it less efficient. The generated bytecode will be identical for both cases - four tests followed by conditional jumps.
Nice, thanks for the tip on the final keyword.
EDIT: if you want to make it more efficient, make the function final. This will avoid the overhead of a virtual method dispatch whenever it's called. |
Author: | sentinel [Sun Jan 02, 2011 7:56 pm ] |
Post subject: | Re: Nesting vs Chaining |
I've been dying to test some new stuff for my android. Will the game be posted? |
Author: | eddieringle [Sun Jan 02, 2011 8:09 pm ] |
Post subject: | Re: Nesting vs Chaining |
Quote: I've been dying to test some new stuff for my android. Will the game be posted?
We're planning to release it on the Market sometime in February.
|
Author: | sentinel [Fri Jan 07, 2011 10:04 am ] |
Post subject: | Re: Nesting vs Chaining |
Nice, be sure to leave a notice! |
Page 1 of 1 | All times are UTC-05:00 |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |