SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,903
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)

    switch-case Vs if else

    I know that switch-case is better than if else from a readability point of view but which one is better when it comes to speed and efficiency of processing?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  2. #2
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know that in C/C++ it depends on the amount of cases there are, if there's only a couple it works like an if else, otherwise it works like a jump I believe.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, some ol' guru can differentitate between the efficiency of the two, but in practice, the ease of use and semantics rules. Use each where suitable.

    And if you are really need to cut such tiny pieces of processing resource, think about getting more hardware power

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    A better alternative then doing micro-optimizations is to profile your code. What i mean by that that is to run portions of the application and dissect the slow parts.

    http://en.wikipedia.org/wiki/Performance_analysis
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Zealot riderbabygurl's Avatar
    Join Date
    Feb 2009
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    switch-case is preferred in fixed options and if else is better in checking truth with in a statement which has a large range..

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    This code:
    PHP Code:
    <?php

    function switchblock $value )
    {
        switch ( 
    $value ) {
            case  
    0: return 'zero';
            case  
    1: return 'one';
            case  
    2: return 'two';
            case  
    3: return 'three';
            case  
    4: return 'four';
            case  
    5: return 'five';
            case  
    6: return 'six';
            case  
    7: return 'seven';
            case  
    8: return 'eight';
            case  
    9: return 'nine';
            default: return 
    'whatever';
        }
    }

    function 
    ifelseblock $value )
    {
        if ( 
    $value === ) return 'zero';
        elseif ( 
    $value === ) return 'one';
        elseif ( 
    $value === ) return 'two';
        elseif ( 
    $value === ) return 'three';
        elseif ( 
    $value === ) return 'four';
        elseif ( 
    $value === ) return 'five';
        elseif ( 
    $value === ) return 'six';
        elseif ( 
    $value === ) return 'seven';
        elseif ( 
    $value === ) return 'eight';
        elseif ( 
    $value === ) return 'nine';
        else return 
    'whatever';
    }

    #-------------------------------------------------------------------------------

    for ( $i 0$i 11$i++ ) {
        
    var_dumpswitchblock$i ), ifelseblock$i ) );
    }
    Executes under 0.2 ms, let me say that again 0.2 ms. That is very very very fast.

    Click to enlarge
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    The switch/case can't be slower than a corresponding series of if/else because the worst way that the switch can be treated is as if it were that series of if statements. Unless you have hundreds of thousands of cases though it probably isn't going to make any noticanble difference.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Plano
    Posts
    643
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    switch statements aren't really what you need to be optimizing, since the time it takes is negligible. the only time it'll really matter is if you're calling a function in your condition statement.

    for example:
    PHP Code:
    if (count($array) == 0){
       return 
    "there are no records";
    } else if (
    count($array) == 1){
       return 
    "there is only one record";
    } else {
       return 
    "yay lots of records";
    }

    switch (
    count($array)){
      case 
    0: return "there are no records";
      case 
    1: return "there is only one record";
      default: return 
    "yay lots of records";

    in this case the switch would be faster as it only requires one evaluation of the test case, while the if block requires the function to be evaluated every time. this can be avoided easily, though, by counting the array beforehand.

    also, this principle applies to loops, the first for loop requires the script to count the array every iteration, while the second one counts it just once.

    PHP Code:
    for ($x 0$x count($array); $x++){
     echo 
    $x;
    }

    $numElements count($array);
    for (
    $y 0$y $numElements$y++){
     echo 
    $y;


  9. #9
    SitePoint Zealot
    Join Date
    Aug 2007
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For 2 options:
    1. Theoretically: 'If-else' statement is faster than 'switch' statement.
    2. Practically: Both are same to execute.

    But the truth is: 'switch' statement is easy to code quickly. If u place 10/15 if-else statements it kills time, but for switch its that easy & fast.

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,595
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by tariq1654 View Post
    For 2 options:
    1. Theoretically: 'If-else' statement is faster than 'switch' statement.
    Why would that be when with two options a switch/case/default is equivalent to an if/else where you know that the operator for the test is ===.

    As the switch statement still provides more default info the worst it can do is to run identically.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

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