-
-
Save thebeline/571864292d2b2afb39a9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$iterations = 1000000; | |
$test_cases = array( | |
array( | |
'name' => 'class_implements, sensitive, uncached', | |
'function' => function($className, $type) { | |
$parents = class_parents($className, true) + class_implements($className, true); | |
return (isset($parents[$type])); | |
} | |
), | |
array( | |
'name' => 'class_implements, sensitive, cached', | |
'function' => function($className, $type) { | |
static $isSubclassFuncCache = array(); | |
if (!array_key_exists($className, $isSubclassFuncCache)) { | |
$isSubclassFuncCache[$className] = class_parents($className, true) + class_implements($className, true); | |
} | |
return (isset($isSubclassFuncCache[$className][$type])); | |
} | |
), | |
array( | |
'name' => 'class_implements, insensitive, uncached', | |
'function' => function($className, $type) { | |
$parents = array_change_key_case(class_parents($className, true) + class_implements($className, true), CASE_LOWER); | |
return (isset($parents[strtolower($type)])); | |
} | |
), | |
array( | |
'name' => 'class_implements, insensitive, cached', | |
'function' => function($className, $type) { | |
static $isSubclassFuncCache = array(); | |
$type = strtolower($type); | |
$caseInsensitiveParent = strtolower($className); | |
if (!array_key_exists($caseInsensitiveParent, $isSubclassFuncCache)) { | |
$isSubclassFuncCache[$caseInsensitiveParent] = array_change_key_case(class_parents($className, true) + class_implements($className, true), CASE_LOWER); | |
} | |
return (isset($isSubclassFuncCache[$caseInsensitiveParent][$type])); | |
} | |
), | |
array( | |
'name' => 'reflection, uncached', | |
'function' => function($className, $type) { | |
if (is_subclass_of($className, $type)) { | |
return true; | |
} | |
if (!interface_exists($type)) { | |
return false; | |
} | |
$r = new ReflectionClass($className); | |
return $r->implementsInterface($type); | |
} | |
), | |
array( | |
'name' => 'reflection, cached', | |
'function' => function($className, $type) { | |
if (is_subclass_of($className, $type)) { | |
return true; | |
} | |
if (!interface_exists($type)) { | |
return false; | |
} | |
static $isSubclassFuncCache = array(); // null as unset, array when set | |
$caseInsensitiveParent = strtolower($className); | |
$caseInsensitiveChild = strtolower($type); | |
if (!array_key_exists($caseInsensitiveParent, $isSubclassFuncCache)) { | |
$r = new ReflectionClass($className); | |
if ($r->implementsInterface($type)) { | |
$isSubclassFuncCache[$caseInsensitiveParent][$caseInsensitiveChild] = true; | |
} | |
} | |
return (isset($isSubclassFuncCache[$caseInsensitiveParent][$caseInsensitiveChild])); | |
} | |
), | |
); | |
// our test cases | |
interface A {} | |
class B implements A {} | |
class C extends B {} | |
class D {} | |
foreach($test_cases as $test) { | |
echo "{$test['name']}\n"; | |
$s = microtime(true); | |
for ($j = 0; $j < $iterations; $j++ ) { | |
$test['function']('B','A'); | |
$test['function']('C','A'); | |
$test['function']('D','A'); | |
} | |
$e = microtime(true); | |
$d = ($e - $s); | |
$p = round(($d / $iterations) * 1000000, 4); | |
echo " $d seconds for $iterations iterations.\n"; | |
echo " {$p}us per call.\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment