Created
November 21, 2011 11:31
-
-
Save pierrejoye/1382376 to your computer and use it in GitHub Desktop.
get_ vs reflection
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 | |
class foo { | |
public $a = 1; | |
private $b = 2; | |
public $c = 3; | |
public $d = 4; | |
protected $e = 5; | |
public function methodA() {} | |
private function methodB() {} | |
public function methodC() {} | |
} | |
$object = new foo; | |
$iterations = 100; | |
$s = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
$props = array(); | |
foreach ($object as $k=>$v) { | |
$props[strtolower($k)] = true; | |
} | |
$result['propertiess'] = $props; | |
$methods = get_class_methods(get_class($object)); | |
$methods = array_flip($methods); | |
array_change_key_case($methods); | |
$result['methods'] = $methods; | |
} | |
$e = microtime(true); | |
echo "Duration get_*: " . ($e - $s) . "\n"; | |
$s = microtime(true); | |
for ($i = 0; $i < $iterations; $i++) { | |
$class = get_class($object); | |
$r = new ReflectionClass($class); | |
$methods = array(); | |
foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { | |
$methods[strtolower($method->getName())] = true; | |
} | |
$props = array(); | |
foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { | |
$props[$property->getName()] = true; | |
} | |
} | |
$e = microtime(true); | |
echo "Duration reflection: " . ($e - $s) . "\n"; | |
-------- | |
g:\php-sdk\php54\vc9\x86\php54>Debug_TS\php -n h:\projects\php\reflection.php | |
Duration get_*: 0.0062789916992188 | |
Duration reflection: 0.01477313041687 | |
g:\php-sdk\php54\vc9\x86\php54>Debug_TS\php -n h:\projects\php\reflection.php | |
Duration get_*: 0.0064659118652344 | |
Duration reflection: 0.014964818954468 | |
g:\php-sdk\php54\vc9\x86\php54>Debug_TS\php -n h:\projects\php\reflection.php | |
Duration get_*: 0.0063018798828125 | |
Duration reflection: 0.015130996704102 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment