Switch vs. If January 27, 2008

Often, with little rhyme or reason behind it, I choose between using a switch condition or an if-else statement while coding simple condition matches in PHP. I got curious about which is actually more efficient at matching a random integer with a set of conditionals. So, I setup a script to create a set of large scripts to test the speed of these different constructs. Using the ‘time’ command, I measured the speed at which the condition could match a random number. Here is what I found:

  The switch construct is generally more than twice as fast at matching a simple integer within a single large set of conditions.

There is largely little difference between the time it takes to find a condition using if,if statements or if-elseif statements. Also the size of the resulting scripts were only negligibly different compared to the difference in execution time.

At first I thought that since the switch statement breaks execution after it matches its value especially since I was using random numbers, that this could account for the time difference, but that also had no bearing. Setting $var=999999 in the below statements had little effect on the faster execution time of the switch statement.

If-Elseif

<?
$var=rand(0,1000000);
if ($var==0)
  {
  echo "found 0";
  }
elseif ($var==1)
  {
  echo "found 1";
  }
elseif ($var==2)
  {
  echo "found 2";
  }
 
....
?>

If,If

<?
$var=rand(0,1000000);
if ($var==1)
  {
  echo "found 1";
  }
if ($var==2)
  {
  echo "found 2";
  }
if ($var==3)
  {
  echo "found 3";
  }
....
?>

Switch

<?
$var=rand(0,1000000);
switch ($var)
  {
   case 1: 
      echo "found 1";
      break;
   case 2: 
      echo "found 2";
      break;
   case 3: 
      echo "found 3";
      break;
....
?>

Also, I wondered if the switch condition would overall be less efficient if there were a large number of switch statements within a script, instead of a single switch construct with many cases. The thinking was that the switch condition was possibly more expensive to setup than an if-else. However, in this case, many switch statements still performed faster (around 25% more) than when using many if-else or if,if conditionals instead. This was tested with 100,000 iterations of 10 conditions as opposed to a 1 iteration of 1,000,000 conditions.

Of course, the if else construct is a bit more powerful for more complex condition testing, but if you only have a simple variable to match up against a known set of values, I guess the winner is the switch condition!

10 Comments
Tom February 19th, 2010

Thanks for this! Was just wondering about the same thing.

pj February 25th, 2010

Thanks a lot! I got to the “PHP Switch” chapter of PHP Basic course at W3Schools.com and immediately wondered about (and googled) the difference between if vs switch. Thanks to you I learned when to use each one!

Also, beginner question: I see you use curly braces { and } even when you only have 1 line to be executed for a certain if or elseif statement. I assume you do this just out of habit?

andre.roesti February 25th, 2010

Every Millisecond counts. Thanks for sharing it.

Thomas April 29th, 2010

Hmm, what was the setup (os, php v, etc), one wonders. Tested 100,000,000 iterations with php 5.3.2 on windows+freebsd+ubuntu server (both scripts noticeably slower in windows – surprise!). Very little difference. Have they changed if vs switch behavior perhaps? @pj: I’d guess yes. Do the same. Easier to read and make changes (debugging e.g.)

Justin May 6th, 2010

Frankly, I couldn’t care less that the switch statement is X milliseconds faster or slower in Y fringe scenarios.

If you’re replacing your if statements with switch statements and each case has a break after it, you’re misusing the language’s features.

In my code, scenarios where switch is the right tool for the job are extremely rare and, when I find myself in such a position, I often ask myself if there’s a better way to do what I’m trying.

This vaguely reminds me of explaining to someone why you don’t use the bold tag in markup, you use the strong tag.

This is all completely beside preference. I have no idea how anyone could find a switch statement prettier than an if/else. PHP has C-style brackets for a reason.

doro June 29th, 2010

i was wondering about the same thing, for some reason lately i like using switch more.. good to see that there’s no speed problem :)

Patryk Januszewski November 8th, 2010

I noticed that you mentioned at the end of your post that the switch is great for value matching, but I’ve also used it for more complex logical matches as well (since everything really just get processed down to a value. I’ve done the following to replace an if-else-if :

switch (true) {
case isset($donkey['face']):

break;
case isset($donkey['pants']):

break;
case isset($donkey['butt']):

break;
}

Just sharing :)

Jamie November 16th, 2010

@Patryk – that may work, but man is it ugly and counter-intuitive.

Felix January 2nd, 2013

nice!! i’m new to php, but it eaven could help me!!
and i learned about the native php “time()” function!

Daniel April 9th, 2013

Interesting article, I usually use if, elseif.

Leave a Reply