PHP Design Patterns .

PHP Design Patterns PHP Performance Tuning if VS switch

PHP Performance Tuning - if /elseif VS switch case


I often use the php switch case statement because I think it looks neater in the code and because I think it is probably more efficient to run. But is it more efficient? I set up this little test to run if / elseif VS switch case 100,000 times each.

The results - if / else was slightly better. While I still think switch / case looks neater it is actually slower. If you are really squeezing all the speed you can get from your application if / elseif may be the way to go.


testPerformanceIfVSSwitch

//copyright Lawrence Truett and FluffyCat.com 2009, all rights reserved
  	define('BR', '<'.'BR'.'>');
	echo 'Performance Testing PHP if / else VS switch with 100,000 iterations'.BR.BR;
	
	$ifTime = 0;
	$switchTime = 0;
	for ($x = 0; $x < 100000; $x++) {
		$oneToFive = rand(1,5);
		if (0 == fmod($x,2)) {
			$switchTime = $switchTime + testSwitch($oneToFive);
			$ifTime = $ifTime + testIf($oneToFive);
		} else {
			$ifTime = $ifTime + testIf($oneToFive);  			
			$switchTime = $switchTime + testSwitch($oneToFive);  		
		}
	}
	echo 'total if / else time: '.$ifTime.BR;
	echo 'total case time: '.$switchTime.BR;
	echo BR;
	if ($ifTime > $switchTime) {
		echo 'switch is quicker by '.($ifTime - $switchTime).BR;
	} else {
		echo 'if / else is quicker by '.($switchTime - $ifTime).BR;		
	}

function testSwitch($oneToFive) { $time_start = microtime(true); switch($oneToFive) { case 1: $z = 1; break; case 2: $z = 2; break; case 3: $z = 3; break; case 4: $z = 4; break; case 5: $z = 5; break; } $time_end = microtime(true); $time = $time_end - $time_start; return $time; } function testIf($oneToFive) { $time_start = microtime(true); if (1 == $oneToFive) { $z = 1; } elseif(2 == $oneToFive) { $z = 2; } elseif(3 == $oneToFive) { $z = 3; } elseif(4 == $oneToFive) { $z = 4; } elseif(5 == $oneToFive) { $z = 5; } $time_end = microtime(true); $time = $time_end - $time_start; return $time; }
To download source right-click here and "Save As...".


output 1 of testPerformanceIfVSSwitch.php

Performance Testing PHP if / else VS switch with 100,000 iterations

total if / else time: 0.66318321228027 total case time: 0.68480849266052

if / else is quicker by 0.021625280380249


output 2 of testPerformanceIfVSSwitch.php

Performance Testing PHP if / else VS switch with 100,000 iterations

total if / else time: 0.66332268714905 total case time: 0.68789744377136

if / else is quicker by 0.024574756622314


output 3 of testPerformanceIfVSSwitch.php

Performance Testing PHP if / else VS switch with 100,000 iterations

total if / else time: 0.6631076335907 total case time: 0.67988753318787

if / else is quicker by 0.016779899597168


output 4 of testPerformanceIfVSSwitch.php

Performance Testing PHP if / else VS switch with 100,000 iterations

total if / else time: 0.66344714164734 total case time: 0.67861771583557

if / else is quicker by 0.015170574188232


output 5 of testPerformanceIfVSSwitch.php

Performance Testing PHP if / else VS switch with 100,000 iterations

total if / else time: 0.66696906089783 total case time: 0.68225598335266

if / else is quicker by 0.015286922454834
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by krishnabhaya on 2009-04-23 Rate this Comment

HI,it's wonder that same script, showing me switch case is quicker in all cases.

I got
Performance Testing PHP if / else VS switch with 100,000 iterations

total if / else time: 0.64044070243835
total case time: 0.59019613265991

switch is quicker by 0.050244569778442
--------------
Performance Testing PHP if / else VS switch with 100,000 iterations

total if / else time: 0.81924557685852
total case time: 0.61534929275513

switch is quicker by 0.20389628410339

so there must be some other factor, which affects the same.

Comment by Larry on 2009-03-17 Rate this Comment

I wondered what increasing the number of if / case statements would do to the results. Increasing from 5 to 10 didn't change the results that much. The total times increased only slightly, and the if / elseif was still faster - although by a smaller margin.

Sample result with 10 choices:

Performance Testing PHP if / else VS switch with 100,000 iterations

total if / else time: 0.69219636917114
total case time: 0.70317435264587

if / else is quicker by 0.010977983474731

 
Sign in to comment on PHP Design Patterns PHP Performance Tuning if VS switch.