I read in a couple of spots that using isset() or empty() is much quicker than doing a simple ($var) check. I was curious how accurate these statements were, so I wrote a little benchmark script and was a bit surprised by the result. Using isset() or empty() was about 40x faster.
Here’s the results from looping 50,000 times performing the different tests, obviously this change won’t make your app that much quicker, but a server with significant load, it’ll help.
Use isset(): 0.02 secs
Use empty(): 0.02 secs
Use ($var): 0.89 secs
Use empty(): 0.02 secs
Use ($var): 0.89 secs
Here’s the test I wrote:
#intialize array $c = 0; $count = 50000;
while ($c < $count) { $a[$c] = $c; ++$c; }
while ($c < $count) { $a[$c] = $c; ++$c; }
## use isset to check
$timer = new mkaz_timer();
for ($i=0; $i < $count; $i++) {
if (isset($a[$c])) { }
}
echo “Use isset(): ” .$timer->stop().”\n”;
$timer = new mkaz_timer();
for ($i=0; $i < $count; $i++) {
if (isset($a[$c])) { }
}
echo “Use isset(): ” .$timer->stop().”\n”;
## use empty to check
$timer = new mkaz_timer();
for ($i=0; $i < $count; $i++) {
if (empty($a[$c])) { }
}
echo “Use empty(): ” .$timer->stop().”\n”;
$timer = new mkaz_timer();
for ($i=0; $i < $count; $i++) {
if (empty($a[$c])) { }
}
echo “Use empty(): ” .$timer->stop().”\n”;
## test for variable
$timer = new mkaz_timer();
for ($i=0; $i < $count; $i++) {
if ($a[$c]) { }
}
echo “Use (\$var): “.$timer->stop().”\n”;
$timer = new mkaz_timer();
for ($i=0; $i < $count; $i++) {
if ($a[$c]) { }
}
echo “Use (\$var): “.$timer->stop().”\n”;
Uses this simple performance class I wrote.
Technorati Tags: php, benchmarking
Trackbacks & Pingbacks 1
Digital Photography
Some portions of this article sounds interesting. May be you have some links where I could read more about this topic?
Post a Comment