Add to Favorites

another look at php error suppression performance

There are many posts out there that talk about how error suppression is a performance hit when used. For a very simple application though, how much of a performance hit are we talking about and in what specific real world instances?

Here is an example of using the error suppression operator in php. In this example we are showing the user's posted value back to them in the form. This is useful when you had an error in what the user supplied and you want them to check their value and re-submit.

<form method="post">
<input type="text" name="firstname" value="<?php echo @$_POST['firstname']; ?>" />
<input type="submit" value="Submit" />
</form>

In reality, how detrimental can this be?

The performance difference between using the suppression operator and not using the suppression operator is so small on a single use scale that in order to get any idea of what kind of hit we are actually taking, the operation has to be repeated over and over in a loop. We time how long that loop takes, and then make some assumptions on the performance effects.

In my testing I used 100,000 iterations in a loop. I compared using isset before echo'ing a value to just using the error suppression when echo'ing a value. I figured in reality, if you were going to use isset consistently to test before echo'ing a value, you would almost undoubtedly create a function to limit the duplication and amount of code for these checks.

There is a difference in testing when an error would have actually been generated vs when there is no error. So in the test script we conduct two tests, one with the array key set, one with the array key unset.

The results show that the performance difference is negligible even over the 100,000 iterations. In actual usage in the way above, the amount of form fields will usually never get very high so actual performance hit there is so minimal it isn't even noticeable. If you are looking for a place to start as far as performance optimization goes, you can hold off on the error suppression operator as a last ditch effort.

However, if you use the error suppression operator in a very large loop or commonly with functional calls the performance hit could be rather large, so definitely use good judgement in those situations.

The code I used for the testing:

<?php

define('TIMES', 100000);

function valisset(&$arr,$v) {
if ( isset($arr[$v]) ) return $arr[$v];
return null;
}

function run_test() {
$start = microtime(true);
for ($i = 0; $i < TIMES; $i++) {
echo @$_POST['foo'.$i];
}
echo '<br />'.(microtime(true) - $start) .'<br /><br />';

$start = microtime(true);
for ($i = 0; $i < TIMES; $i++) {
echo valisset($_POST,'foo'.$i);
}
echo '<br />'.(microtime(true) - $start) .'<br /><br />';
}

echo 'values unset<br />';
run_test();

$_POST = array();
for ($i = 0; $i < TIMES; $i++) {
$_POST['foo'.$i] = 'a';
}

echo str_repeat('-',400) .'<br />';
echo 'values set<br />';
run_test();

?>

Comments

Be the first to leave a comment on this post.

Leave a comment

To leave a comment, please log in / sign up