Skip to content

Instantly share code, notes, and snippets.

@thebeline
Forked from weierophinney/test.php
Created December 10, 2014 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thebeline/571864292d2b2afb39a9 to your computer and use it in GitHub Desktop.
Save thebeline/571864292d2b2afb39a9 to your computer and use it in GitHub Desktop.
<?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