// Define the number of tests that we want to perform $tests = 4; // Number of loops to go through on each test // The more tests the more accurate the results $loops = 1000000; // Make sure that we don't die early due to max_execution_time or running out of memory ini_set('max_execution_time', 300); ini_set('memory_limit', '128M'); // Turn off error reporting to eliminate the potential that the actual logging // of errors might affect the result ini_set('error_reporting', 0); // You can pass pct_set in via the get request to vary the percentage // of values that are 'set' $pct_set = isset($_REQUEST['pct_set']) ? $_REQUEST['pct_set'] : 50; function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } echo "Using percent set = $pct_set\n"; for ($testno = 0; $testno < $tests; $testno++) { $conf = array(); // Populate the array with the specified number of values for ($i = 1; $i < $loops; $i++) { if( rand(1, 100) <= $pct_set) { $conf[$i] = true; } } $t1 = microtime_float(); // Using the syntax that generates a PHP Notice if $conf[$i] is unset for ($i = 1; $i < $loops; $i++) { if ($conf[$i]) {} } $t2 = microtime_float(); // Use just isset() - NOTE that this test is not the same logical test // as the one above. It will return true if the value is set to an 'empty' // value such as 0 or false. for ($i = 1; $i < $loops; $i++) { if (isset($conf[$i])) {} } $t3 = microtime_float(); // The long syntax with two conditions for ($i = 1; $i < $loops; $i++) { if (isset($conf[$i]) && $conf[$i]) {} } $t4 = microtime_float(); // Use empty(). This returns false if the value is either not set // or contains a false value such as 0, false, or array() // see http://php.net/empty for ($i = 1; $i < $loops; $i++) { if (!empty($conf[$i])) {} } $t5 = microtime_float(); // Now display our results: ?>
With NOTICE:
With isset: / Diff:
With both: / Diff:
with !empty: / Diff: