Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Created August 15, 2012 12:40
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mindplay-dk/3359812 to your computer and use it in GitHub Desktop.
Save mindplay-dk/3359812 to your computer and use it in GitHub Desktop.
A benchmark of reflection API performance in PHP
<?php
/**
* Benchmark: Reflection Performance
*
* Conclusion: there is no performance-gain from caching reflection-objects.
*/
define('NUM_TESTS', 10);
header('Content-type: text/plain');
$func = function($a, $b, $c) {
// ...
};
class Foo
{
public $a;
protected $b;
private $c;
public function foo($a,$b,$c) {}
protected function bar($a,$b,$c) {}
private function baz($a,$b,$c) {}
}
for ($i=0; $i<NUM_TESTS; $i++) {
$start = microtime(true);
$ref = new ReflectionClass($func);
$end = microtime(true);
echo "ReflectionClass # $i: " . number_format(1000000*($end-$start), 3) . " µsec\n";
}
for ($i=0; $i<NUM_TESTS; $i++) {
$start = microtime(true);
$ref = new ReflectionFunction($func);
$end = microtime(true);
echo "ReflectionFunction # $i: " . number_format(1000000*($end-$start), 3) . " µsec\n";
}
class Cache
{
private $cache = array();
public function getReflection($class)
{
if (!isset($this->cache[$class])) {
$this->cache[$class] = new ReflectionClass($class);
}
return $this->cache[$class];
}
}
$cache = new Cache;
for ($i=0; $i<NUM_TESTS; $i++) {
$start = microtime(true);
$ref = $cache->getReflection('Foo');
$end = microtime(true);
echo "Cached ReflectionClass # $i: " . number_format(1000000*($end-$start), 3) . " µsec\n";
}
@TomZhuPlanetart
Copy link

Is that saying that Reflection almost does not affect the performance on PHP 8.1 and 7.2 ?

@sunxyw
Copy link

sunxyw commented Aug 7, 2023

Is that saying that Reflection almost does not affect the performance on PHP 8.1 and 7.2 ?

I think it has a very minimal impact, so unless you're in a performance-critical scenario, I don't think you need to worry about reflections negatively affecting your application.

@eusonlito
Copy link

PHP 8.2.10:

ReflectionClass # 0: 1.907 µsec
ReflectionClass # 1: 0.954 µsec
ReflectionClass # 2: 0.000 µsec
ReflectionClass # 3: 0.000 µsec
ReflectionClass # 4: 0.000 µsec
ReflectionClass # 5: 0.000 µsec
ReflectionClass # 6: 0.000 µsec
ReflectionClass # 7: 0.000 µsec
ReflectionClass # 8: 0.000 µsec
ReflectionClass # 9: 0.000 µsec
ReflectionFunction # 0: 0.000 µsec
ReflectionFunction # 1: 0.000 µsec
ReflectionFunction # 2: 0.000 µsec
ReflectionFunction # 3: 0.000 µsec
ReflectionFunction # 4: 0.000 µsec
ReflectionFunction # 5: 0.000 µsec
ReflectionFunction # 6: 0.000 µsec
ReflectionFunction # 7: 0.000 µsec
ReflectionFunction # 8: 0.000 µsec
ReflectionFunction # 9: 0.000 µsec
Cached ReflectionClass # 0: 2.861 µsec
Cached ReflectionClass # 1: 0.000 µsec
Cached ReflectionClass # 2: 0.000 µsec
Cached ReflectionClass # 3: 0.000 µsec
Cached ReflectionClass # 4: 0.000 µsec
Cached ReflectionClass # 5: 0.000 µsec
Cached ReflectionClass # 6: 0.000 µsec
Cached ReflectionClass # 7: 0.000 µsec
Cached ReflectionClass # 8: 0.000 µsec
Cached ReflectionClass # 9: 0.000 µsec
PHP 8.2.10 (cli) (built: Sep  2 2023 06:59:22) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.2.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.2.10, Copyright (c), by Zend Technologies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment