- microtime: documentation ( source)
<?php
$foo = array();
function slow($foo) {
// Slower
if (isset($foo) && is_array($foo)) { return true; }
}
function fast($foo) {
// Faster
if (isset($foo)) { if (is_array($foo)) { return true; } }
}
function benchmark($callback, $times) {
echo "Testing $callback - Valid\n";
$s = microtime(true);
for ($i = 0; $i < $times; $i++) {
$callback(array(1, 2, 3));
}
$e = microtime(true);
echo "Completed in " . ($e - $s) . " Seconds\n";
echo "Testing $callback - Invalid\n";
$s = microtime(true);
for ($i = 0; $i < $times; $i++) {
$callback("string");
}
$e = microtime(true);
echo "Completed in " . ($e - $s) . " Seconds\n";
echo "Testing $callback - null\n";
$s = microtime(true);
for ($i = 0; $i < $times; $i++) {
$callback(null);
}
$e = microtime(true);
echo "Completed in " . ($e - $s) . " Seconds\n";
}
benchmark("slow", 1000);
benchmark("fast", 1000);