Last visit was: It is currently Thu Dec 05, 2024 10:27 am


All times are UTC-05:00




Post new topic Reply to topic  [6 posts ] 
    Author Message
     Post subject:Nesting vs Chaining
    PostPosted:Thu Dec 30, 2010 2:55 pm 
    User avatar
     

    Joined:Sun Feb 12, 2006 8:56 pm
    Posts:1019
    Website:http://eddieringle.com
    Location:Detroit, MI
    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; }

    _________________
    -- Eddie Ringle

    Check out Elysian Shadows and consider backing us on Kickstarter!

    ====================================

    Image


    Top
    Offline  
     Post subject:Re: Nesting vs Chaining
    PostPosted:Sat Jan 01, 2011 11:54 am 
    Connoisseur of the Godawful
    User avatar
     

    Joined:Tue Mar 01, 2005 9:00 am
    Posts:456
    ICQ:286315965
    Website:http://rabidtinker.mine.nu/
    Yahoo Messenger:alistair_lynn
    AOL:Agent_Vast@mac.com
    Location:127.0.0.1
    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.

    _________________
    Alastair Lynn / Alumnus / Onlink Team


    Top
    Offline  
     Post subject:Re: Nesting vs Chaining
    PostPosted:Sat Jan 01, 2011 12:51 pm 
    User avatar
     

    Joined:Sun Feb 12, 2006 8:56 pm
    Posts:1019
    Website:http://eddieringle.com
    Location:Detroit, MI
    Quote:
    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.
    Nice, thanks for the tip on the final keyword.

    _________________
    -- Eddie Ringle

    Check out Elysian Shadows and consider backing us on Kickstarter!

    ====================================

    Image


    Top
    Offline  
     Post subject:Re: Nesting vs Chaining
    PostPosted:Sun Jan 02, 2011 7:56 pm 
    User avatar
     

    Joined:Sat Jun 06, 2009 1:21 am
    Posts:581
    I've been dying to test some new stuff for my android. Will the game be posted? :D

    _________________
    I'll see you on the dark side of the (ferrous) moon.


    Top
    Offline  
     Post subject:Re: Nesting vs Chaining
    PostPosted:Sun Jan 02, 2011 8:09 pm 
    User avatar
     

    Joined:Sun Feb 12, 2006 8:56 pm
    Posts:1019
    Website:http://eddieringle.com
    Location:Detroit, MI
    Quote:
    I've been dying to test some new stuff for my android. Will the game be posted? :D
    We're planning to release it on the Market sometime in February.

    _________________
    -- Eddie Ringle

    Check out Elysian Shadows and consider backing us on Kickstarter!

    ====================================

    Image


    Top
    Offline  
     Post subject:Re: Nesting vs Chaining
    PostPosted:Fri Jan 07, 2011 10:04 am 
    User avatar
     

    Joined:Sat Jun 06, 2009 1:21 am
    Posts:581
    Nice, be sure to leave a notice! :classy:

    _________________
    I'll see you on the dark side of the (ferrous) moon.


    Top
    Offline  
    Display posts from previous: Sort by 
    Post new topic Reply to topic

      All times are UTC-05:00


      Who is online

      Users browsing this forum: No registered users and 1 guest


      You cannot post new topics in this forum
      You cannot reply to topics in this forum
      You cannot edit your posts in this forum
      You cannot delete your posts in this forum
      You cannot post attachments in this forum

      Search for:
      Jump to:  
      cron
      Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
      Theme created by Miah with assistance from hyprnova