20
Sep
07

Conditional Assignment Efficiency in PHP






I got curious and did a little experiment today, which was by no means thoroughly scientific, but it did show some interesting results. I started by considering one of my favorite operators - the ternary conditional operator, and wondering if it was as efficient as the alternative if-else statement.

What I ended up setting up was a cute little piece of code that looks like this:

$mtime = microtime();
$mtime = explode(' ', $mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;      

$testVar = true;
$string = "";
for($i=0; $i<5000000; $i++){
	if($testVar)
	{
		$string = "true";
	}
	else
	{
		$string = "false";
	}
}

$mtime = microtime();
$mtime = explode(" ", $mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo 'This assignment took ' .$totaltime. ' seconds.';

All that the code at the beginning and end with $mtime does is check how long the function in the middle took, and in this case, the function in the middle was my first test. The timer script was borrowed from DeveloperTutorials.com, if any of you were curious, and it’s incredibly useful.

Using this sort of really light framework, I conducted three tests of 5 million string assignments and recorded ten times for each, then averaged the times for each test. I also attempted each test twice, setting the conditional variable “$testVar” to both true and false, and the differences were quite interesting. Here’s the code and what I found.

Test A:

$testVar = true;
$string = "";
for($i=0; $i<5000000; $i++){
	if($testVar)
	{
		$string = "true";
	}
	else
	{
		$string = "false";
	}
}

This is the generic if-else statement. With $testVar set to true, the average time was 0.882983208 seconds, and with it set to false, the average time was 0.894877338 seconds, a fairly small, but consistent, difference.

Test B:

$testVar = true;
$string = "";
for($i=0; $i<5000000; $i++){
	$string = "false";
	if($testVar)
	{
		$string = "true";
	}
}

This might be a little more complicated, but not much. It basically defaults the $string variable to false, and then checks to see whether or not it’s actually true, thereby removing the need for an else statement. The average time for $testVar set to true was 1.348060751 seconds, and for false it was 0.797976613 seconds, which ended up being by far our longest and shortest times.

Test C:

$testVar = true;
$string = "";
for($i=0; $i<5000000; $i++){
	$string = ( $testVar ) ? "true" : "false";
}

Here’s my little friend, the ternary conditional operator. For those of you not familiar with this syntax, all it says is “if what’s in these parenthesis is true, we assign $string the value before the colon, otherwise we set it to the value after the colon” all in one simple line of code. It’s really nice if your code is all sticky and messy from too many if-else assignment statements, but how did it do? With $testVar set to true, we averaged 0.862836647 seconds for five million assignments, and 0.851820135 seconds for $testVar set to false.

What does it all mean then? It means that if you’re going to be doing five million conditional assignments on a page, with the bulk of those tests evaluating to false similarly to Test B, go with the syntax in Test B. In reality, however, it means that the difference between methods of simple conditional assignment is so negligible, that at the very most you sacrifice half a second of processing time over the course of five million assignments. My preference? Stick with the ternary conditional operator - it’s clean, handy, and won’t kick your dog.

The interesting part though is that apparently “going into” an if statement has a cost associated with it. Test A does a “check then assign” method wherein it checks if the test value is true and if so, goes into the if statement and assigns the string value. However, Test B’s false case does an “assign then check” method that first assigns the string value, then checks the test value and skips the “true” assignment. Effectively, the only real difference between the two that I can see is that Test A “goes into” the if statement, and Test B does not.

Am I wrong here? Who else knows about how the PHP interpreter works in this regard? I’m curious.


3 Responses to “Conditional Assignment Efficiency in PHP”


  1. 1 Killroy May 4th, 2008 at 1:11 pm

    Have you considered that you are doing different operations? Assigning “true”, a 4 character string and “false” a 5 character string respectively. You could simply be seeing the difference in memory allocation. Why not try assigning the same value to give the true part of the if and the false part an equal chance?

  2. 2 Matt Di Pasquale May 8th, 2009 at 12:50 pm

    the true false thing might make sense above. it would be better to use 0 or 1.

    my guesses for why the B false case is faster than the A false case is that in A, the computer (1) has to store more instructions (because there are more lines) and (2) has to skip the first if statement since $testVar is false… skipping lines of code can cause a delay. In B, no lines are skipped. I wonder what happens if you switch the order of the conditions. Might not make a difference. Then, I’d say the difference mainly applies to (1).

    Also, in A and C, you only do 1 assignment, but in B, if it’s true, you have to do 2 assignments instead of only 1. I think assignments take more time than a simple comparison. Deciding whether or not to “go into” an if clause only requires a simple comparison (well, in the above cases). A simple comparison usually only takes one cycle. Assignment, however, takes more than one cycle (3 maybe?), hence the huge jump in time when 2 assignments are done.

  3. 3 testing` Mar 11th, 2010 at 11:20 am

    This is a test. Feel free to delete.

Leave a Reply




Close
E-mail It