Thread: Whats is more efficient?

Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1 Whats is more efficient? 
    Donator

    Join Date
    Jul 2009
    Posts
    1,452
    Thanks given
    27
    Thanks received
    13
    Rep Power
    69
    Is it having an if statement looking like this:
    Code:
    if (alldis || alldat || nevermind || goodluck || pokemon) {
    }
    or is it having a switch and an if statement?
    Code:
    switch (lele) {
    case alldis:
    break;
    case alldat:
    break;
    case nevermind:
    break;
    case goodluck:
    break;
    }
    if (pokemon) {
    }
    Whats more efficient of the two?
    Reply With Quote  
     

  2. #2  
     

    Vastiko's Avatar
    Join Date
    Dec 2006
    Posts
    5,700
    Thanks given
    300
    Thanks received
    663
    Rep Power
    5000
    If you want the same result I guess the first one.

    For different results use the switch :x
    Reply With Quote  
     

  3. #3  
    Donator

    Join Date
    Jul 2009
    Posts
    1,452
    Thanks given
    27
    Thanks received
    13
    Rep Power
    69
    Quote Originally Posted by Michael View Post
    If you want the same result I guess the first one.

    For different results use the switch :x
    All i need is to return is true or false, but i wish to know which of the methods that uses the less CPU, and is most effective
    Reply With Quote  
     

  4. #4  
    Registered Member
    Hunter's Avatar
    Join Date
    Jun 2009
    Age
    33
    Posts
    857
    Thanks given
    3
    Thanks received
    23
    Rep Power
    216
    Quote Originally Posted by Masochist View Post
    All i need is to return is true or false, but i wish to know which of the methods that uses the less CPU, and is most effective
    Honestly it depends on personal preference. I've never really noticed any increase or decrease in efficiency on either. Although with switches - you could use 'em like...

    Code:
    public boolean checkCrap(int crap) {
         switch(crap) {
              case 0:
              case 1:
              case 2:
                   return true;
     
              default:
                   return false;
         }
    }
    So I guess I'd choose switch for what you want to do...but that's just me.
    Reply With Quote  
     

  5. #5  
    Registered Member

    Join Date
    Feb 2009
    Age
    27
    Posts
    2,861
    Thanks given
    127
    Thanks received
    226
    Rep Power
    700
    Code:
    public boolean checkCrap(int crap) {
         switch(crap) {
              case 0:
              case 1:
              case 2:
                   return true;
              break;
    
              default:
                   return false;
              break;
         }
    }
    You forgot your breaks.
    Reply With Quote  
     

  6. #6  
    Registered Member
    Hunter's Avatar
    Join Date
    Jun 2009
    Age
    33
    Posts
    857
    Thanks given
    3
    Thanks received
    23
    Rep Power
    216
    Quote Originally Posted by Jarba View Post
    Code:
    public boolean checkCrap(int crap) {
         switch(crap) {
              case 0:
              case 1:
              case 2:
                   return true;
              break;
    
              default:
                   return false;
              break;
         }
    }
    You forgot your breaks.
    You don't need break; when returning info.
    Reply With Quote  
     

  7. #7  
    Registered Member

    Join Date
    Feb 2009
    Age
    27
    Posts
    2,861
    Thanks given
    127
    Thanks received
    226
    Rep Power
    700
    Oh, ok, I thought you did.
    Reply With Quote  
     

  8. #8  
    Registered Member D4NyZ's Avatar
    Join Date
    Sep 2008
    Age
    28
    Posts
    664
    Thanks given
    50
    Thanks received
    10
    Rep Power
    30
    IF = TEH POWER!!!













    rly.
    Spoiler for Retard below:

    Quote Originally Posted by dani_gonzales View Post
    dany i know u hate me because i am better then u,,,so stfu noob and stop spaming my posts or i conntact a moderator!

    Reply With Quote  
     

  9. #9  
    Registered Member

    Join Date
    Feb 2009
    Age
    27
    Posts
    2,861
    Thanks given
    127
    Thanks received
    226
    Rep Power
    700
    For a lot of different results, use a switch. For a few (up to 10) different results, use if-then-else statements.
    Can't remember where I saw this.
    Reply With Quote  
     

  10. #10  
    Community Veteran


    Join Date
    Jan 2008
    Posts
    2,659
    Thanks given
    494
    Thanks received
    627
    Rep Power
    980
    Quote Originally Posted by Masochist View Post
    All i need is to return is true or false, but i wish to know which of the methods that uses the less CPU, and is most effective
    Neither is going to give you a significant performance gain from the other.

    Unless you're going to write a lot of if statements, I would go with option 1. In the end though, it's just syntactic sugar (unless you want to look at the bytecode).
    ~iKilem
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •