Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active October 6, 2023 14:47
Show Gist options
  • Select an option

  • Save eusonlito/08392c110e593f33c9bb84f42652550a to your computer and use it in GitHub Desktop.

Select an option

Save eusonlito/08392c110e593f33c9bb84f42652550a to your computer and use it in GitHub Desktop.
PHP ReflectionFunction and ReflectionClass benchmarks
<?php
/**
* Benchmark: Reflection Performance
*/
define('TESTS', 1_000_000);
header('Content-type: text/plain');
function percent(float $old, float $new): string
{
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 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) {}
}
$class = new Foo();
$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 array $cache = [];
public function getReflection(object $class)
{
return $this->cache[$class::class] ??= new ReflectionClass($class);
}
}
$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)." fastest)\n";
//********************** ReflectionClass String ******************************
$start = microtime(true);
for ($i = 0; $i < TESTS; $i++) {
$ref = new ReflectionClass(Foo::class);
}
$ReflectionClassString = microtime(true) - $start;
echo "ReflectionClass String ".sprintf('%0.4f', $ReflectionClassString)." seconds\n";
//********************** ReflectionClass String Cached ******************************
class CacheObjectString
{
private array $cache = [];
public function getReflection(string $class)
{
return $this->cache[$class] ??= new ReflectionClass($class);
}
}
$cache = new CacheObjectString();
$start = microtime(true);
for ($i = 0; $i < TESTS; $i++) {
$ref = $cache->getReflection(Foo::class);
}
$ReflectionClassStringCached = microtime(true) - $start;
echo "ReflectionClass String Cached String ".sprintf('%0.4f', $ReflectionClassStringCached)." seconds";
echo " (".percent($ReflectionClassString, $ReflectionClassStringCached)." fastest)\n";
echo "\n";
Testing with 1,000,000 iterations
ReflectionFunction 0.05292 seconds
ReflectionClass Object 0.04783 seconds
ReflectionClass Object Cached 0.03557 seconds (26% fastest)
ReflectionClass String 0.05029 seconds
ReflectionClass String Cached String 0.03031 seconds (40% fastest)
------------------------------
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