Spent 0.000162124633789 seconds. Using: if (isset($foo[5]))
Spent 0.000566005706787 seconds. Using: if (isset($foo) && strlen($foo)>4)
Spent 0.000502109527588 seconds. Using: if (strlen($foo)>4)
<?
error_reporting(E_ALL);
$foo = str_repeat('pweew', 1);
$loop = 1000;
// Alternative syntax for: if (isset($foo) && strlen($foo) >= 5)
if (!isset($foo[4]))
echo 'Foo is too short.';
// Benchmarking isset()
$timeS = timer();
for($i=0; $i<$loop; $i++) {
if (isset($foo[5]))
$f='b';
}
$timeE = timer();
echo "\n<br />".'Spent '. ($timeE - $timeS) .' seconds. Using: if (isset($foo[5]))';
// Benchmarking isset and strlen
$timeS = timer();
for($i=0; $i<$loop; $i++) {
if (isset($foo) && strlen($foo)>4)
$f='b';
}
$timeE = timer();
echo "\n<br />".'Spent '. ($timeE - $timeS) .' seconds. Using: if (isset($foo) && strlen($foo)>4)';
// Benchmarking only strlen (asuming a variable is already set)
$timeS = timer();
for($i=0; $i<$loop; $i++) {
if (strlen($foo)>4)
$f='b';
}
$timeE = timer();
echo "\n<br />".'Spent '. ($timeE - $timeS) .' seconds. Using: if (strlen($foo)>4)';
// the timer
function timer() {
$mtime = microtime();
$mtime = explode(' ', $mtime);
return $mtime[1] + $mtime[0];
}
echo "<br /><br />\n\n";
show_source(__FILE__);
?>