which statement is faster, elseif() or switch() ?

A couple of days ago I was out on a run and I starting wondering which statement was faster, a elseif() or a switch().  I am a big fan of switch statements.  I like how clean they look and its really easy to add a new condition to them.  I also assumed that they were much quicker than elseif statements.  So I set up this test to try to prove to myself that I was right.

Here is the script that I used to test my theory. You can download this script and the script timer here.  How it works its we have 3 loops. In the first loop we pick a value randomly out of our test data array.  Then we loop through each statement type(switch and elseif) the value of timing_loops. While doing that we record the length of time it takes to compare the values.  Then we record the tested values from our test data array and the method that was faster by how many microseconds.  Its a pretty simple script, and my no means the best I have written.


 * @author Jesse Baird <jebaird@gmail.com>
 * @since 12/1/2008

require 'PHP_script_timer.php';

$test = array();

$test[0] = 'cat';
$test[1] = 'dog';
$test[2] = 'frog';
$test[3] = 'goat';
$test[4] = 'pig';
$test[5] = 'sometingreallylong';
$test[6] = time(); //this one will be used for an else/default

$fhandle= fopen('elseif_vs_switch_benchmark.log','a'); 


//get the val to test on this loop
$tval = $test[rand(0, 6)];
for ($current_loop = 0; $current_loop < timing_loops; $current_loop++) {

$time_start = microtime_float();
switch($tval) {
    case 'cat':
    case 'dog':
    case 'frog':
    case 'goat':
    case 'pig':
    case 'sometingreallylong':

$timing[] = microtime_float() - $time_start;
echo '<b>switch</b><br>';
$switchAve= timing($timing);


for ($current_loop2 = 0; $current_loop2 < timing_loops; $current_loop2++) {

$time_start = microtime_float();

if($tval == 'cat') {
} elseif($tval == 'dog') {
} elseif($tval == 'frog') {
} elseif($tval == 'goat') {
} elseif($tval == 'pig') {
} elseif($tval == 'sometingreallylong') {
} else {
$timing[] = microtime_float() - $time_start;
echo '<b>else if</b><br>';
$elseIFAve= timing($timing);

//you can use this level if loging if you like

//echo 'tested val: '.$tval."rn";
////echo $elseIFAve;
//printf("switch method average %f mirco secs faster",((float)$elseIFAve-(float)$switchAve));
////echo 'switch method average '.($elseIFAve-$switchAve).' mirco secs faster';
//printf("elseif method average %f mirco secs faster",((float)$switchAve-(float)$elseIFAve));
//echo "rnrn";
////echo 'elseif method average '.($switchAve-$elseIFAve).' mirco secs faster';

//add some info the the log
echo 'ran '.$times_to_run." timesrn tested values:rn".var_export($valsTested)."rn";
//echo $elseIFAve;
printf("switch method average %f mirco secs faster",((float)$elseIFTotalAve-(float)$switchTotalAve));
//echo 'switch method average '.($elseIFAve-$switchAve).' mirco secs faster';
printf("elseif method average %f mirco secs faster",((float)$switchTotalAve-(float)$elseIFTotalAve));
echo "rnrn";
//printf("switch method average %f mirco secs faster",($elseIFAve-$switchAve));


I ran the test script 18 times.  The switch statement was faster 9 times v.s. 8 for the elseif.  Here is a quick break down of the data and somethings that I noticed while testing/writing this script.

  • The sum of the total time taken by the switch statement was: 0.000402 microseconds
  • The sum of the total time taken by the elseif statement was: 0.000628 microseconds
  • The elseif compared values quicker if the value was close to the top of the statement.  While the switch seemed to test all values at the same speed. Thus it might make sense to use an elseif if you know the possible value that is going to be used the most. I.E. if your going to be using the 'cat' statement.

 Looking that the data trends it appears that the switch is the better statement to use if one value is not favored over an another.  I'm still going to stick with my switch statements.  I highly suggest that you run this test your self and see what you come up with.  Change the values, use math computations, etc, its all a matter a preference.