Last active
October 6, 2023 14:47
-
-
Save eusonlito/08392c110e593f33c9bb84f42652550a to your computer and use it in GitHub Desktop.
PHP ReflectionFunction and ReflectionClass benchmarks
This file contains 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 | |
/** | |
* Benchmark: Reflection Performance | |
*/ | |
define('TESTS', 1000000); | |
header('Content-type: text/plain'); | |
function percent($old, $new) | |
{ | |
return round(abs(($new - $old) / $old * 100)).'%'; | |
} | |
echo "\nTesting with ".number_format(TESTS)." iterations\n\n"; | |
//********************** ReflectionFunction ****************************** | |
$func = function($a, $b, $c) {}; | |
$start = microtime(true); | |
for ($i = 0; $i < TESTS; $i++) { | |
$ref = new ReflectionFunction($func); | |
} | |
$end = microtime(true); | |
echo "ReflectionFunction ".sprintf('%0.4f', $end - $start)." seconds\n"; | |
//********************** ReflectionClass Object ****************************** | |
class FooClass | |
{ | |
public $a; | |
protected $b; | |
private $c; | |
public function foo($a, $b, $c) | |
{} | |
protected function bar($a, $b, $c) | |
{} | |
private function baz($a, $b, $c) | |
{} | |
} | |
$class = new FooClass(); | |
$start = microtime(true); | |
for ($i = 0; $i < TESTS; $i++) { | |
$ref = new ReflectionClass($class); | |
} | |
$ReflectionClassObject = microtime(true) - $start; | |
echo "ReflectionClass Object ".sprintf('%0.4f', $ReflectionClassObject)." seconds\n"; | |
//********************** ReflectionClass Object Cached ****************************** | |
class CacheObject | |
{ | |
private $cache = []; | |
public function getReflection($class) | |
{ | |
$name = get_class($class); | |
if (empty($this->cache[$name])) { | |
$this->cache[$name] = new ReflectionClass($class); | |
} | |
return $this->cache[$name]; | |
} | |
} | |
$cache = new CacheObject(); | |
$start = microtime(true); | |
for ($i = 0; $i < TESTS; $i++) { | |
$ref = $cache->getReflection($class); | |
} | |
$ReflectionClassObjectCached = microtime(true) - $start; | |
echo "ReflectionClass Object Cached ".sprintf('%0.4f', $ReflectionClassObjectCached)." seconds"; | |
echo " (".percent($ReflectionClassObject, $ReflectionClassObjectCached)." faster)\n"; | |
//********************** ReflectionClass String ****************************** | |
$name = get_class($class); | |
$start = microtime(true); | |
for ($i = 0; $i < TESTS; $i++) { | |
$ref = new ReflectionClass($name); | |
} | |
$ReflectionClassString = microtime(true) - $start; | |
echo "ReflectionClass String ".sprintf('%0.4f', $ReflectionClassString)." seconds\n"; | |
//********************** ReflectionClass String Cached ****************************** | |
class CacheObjectString | |
{ | |
private $cache = []; | |
public function getReflection($class) | |
{ | |
if (empty($this->cache[$class])) { | |
$this->cache[$class] = new ReflectionClass($class); | |
} | |
return $this->cache[$class]; | |
} | |
} | |
$cache = new CacheObjectString(); | |
$start = microtime(true); | |
for ($i = 0; $i < TESTS; $i++) { | |
$ref = $cache->getReflection($name); | |
} | |
$ReflectionClassStringCached = microtime(true) - $start; | |
echo "ReflectionClass String Cached ".sprintf('%0.4f', $ReflectionClassStringCached)." seconds"; | |
echo " (".percent($ReflectionClassString, $ReflectionClassStringCached)." faster)\n"; | |
echo "\n"; |
This file contains 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
$> php5.6 benchmark.php | |
Testing with 1,000,000 iterations | |
ReflectionFunction 0.2294 seconds | |
ReflectionClass Object 0.2387 seconds | |
ReflectionClass Object Cached 0.2307 seconds (3% faster) | |
ReflectionClass String 0.2579 seconds | |
ReflectionClass String Cached 0.1479 seconds (43% faster) | |
$> php7.3 benchmark.php | |
Testing with 1,000,000 iterations | |
ReflectionFunction 0.0951 seconds | |
ReflectionClass Object 0.0897 seconds | |
ReflectionClass Object Cached 0.0468 seconds (48% faster) | |
ReflectionClass String 0.1174 seconds | |
ReflectionClass String Cached 0.0407 seconds (65% faster) | |
$> php7.4 benchmark.php | |
Testing with 1,000,000 iterations | |
ReflectionFunction 0.0682 seconds | |
ReflectionClass Object 0.0500 seconds | |
ReflectionClass Object Cached 0.0416 seconds (17% faster) | |
ReflectionClass String 0.0766 seconds | |
ReflectionClass String Cached 0.0385 seconds (50% faster) | |
$> php8.0 benchmark.php | |
Testing with 1,000,000 iterations | |
ReflectionFunction 0.0536 seconds | |
ReflectionClass Object 0.0497 seconds | |
ReflectionClass Object Cached 0.0420 seconds (15% faster) | |
ReflectionClass String 0.0840 seconds | |
ReflectionClass String Cached 0.0367 seconds (56% faster) | |
$> php8.1 benchmark.php | |
Testing with 1,000,000 iterations | |
ReflectionFunction 0.0527 seconds | |
ReflectionClass Object 0.0488 seconds | |
ReflectionClass Object Cached 0.0428 seconds (12% faster) | |
ReflectionClass String 0.0508 seconds | |
ReflectionClass String Cached 0.0368 seconds (28% faster) | |
$> php8.2 benchmark.php | |
Testing with 1,000,000 iterations | |
ReflectionFunction 0.0539 seconds | |
ReflectionClass Object 0.0491 seconds | |
ReflectionClass Object Cached 0.0423 seconds (14% faster) | |
ReflectionClass String 0.0511 seconds | |
ReflectionClass String Cached 0.0367 seconds (28% faster) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment