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.

Revisions

  1. eusonlito revised this gist Oct 6, 2023. 1 changed file with 55 additions and 10 deletions.
    65 changes: 55 additions & 10 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,59 @@
    $> 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.05292 seconds
    ReflectionClass Object 0.04783 seconds
    ReflectionClass Object Cached 0.03557 seconds (26% faster)
    ReflectionClass String 0.05029 seconds
    ReflectionClass String Cached 0.03031 seconds (40% faster)
    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

    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
    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)
  2. eusonlito revised this gist Oct 6, 2023. 1 changed file with 31 additions and 19 deletions.
    50 changes: 31 additions & 19 deletions benchmark.php
    Original file line number Diff line number Diff line change
    @@ -4,11 +4,11 @@
    * Benchmark: Reflection Performance
    */

    define('TESTS', 1_000_000);
    define('TESTS', 1000000);

    header('Content-type: text/plain');

    function percent(float $old, float $new): string
    function percent($old, $new)
    {
    return round(abs(($new - $old) / $old * 100)).'%';
    }
    @@ -17,7 +17,7 @@ function percent(float $old, float $new): string

    //********************** ReflectionFunction ******************************

    $func = function(stdClass $a, Closure $b, float $c): void {};
    $func = function($a, $b, $c) {};

    $start = microtime(true);

    @@ -31,23 +31,23 @@ function percent(float $old, float $new): string

    //********************** ReflectionClass Object ******************************

    class Foo
    class FooClass
    {
    public stdClass $a;
    protected Closure $b;
    private float $c;
    public $a;
    protected $b;
    private $c;

    public function foo(stdClass $a, Closure $b, float $c): void
    public function foo($a, $b, $c)
    {}

    protected function bar(stdClass $a, Closure $b, float $c): void
    protected function bar($a, $b, $c)
    {}

    private function baz(stdClass $a, Closure $b, float $c): void
    private function baz($a, $b, $c)
    {}
    }

    $class = new Foo();
    $class = new FooClass();

    $start = microtime(true);

    @@ -63,11 +63,17 @@ private function baz(stdClass $a, Closure $b, float $c): void

    class CacheObject
    {
    private array $cache = [];
    private $cache = [];

    public function getReflection(object $class): ReflectionClass
    public function getReflection($class)
    {
    return $this->cache[$class::class] ??= new ReflectionClass($class);
    $name = get_class($class);

    if (empty($this->cache[$name])) {
    $this->cache[$name] = new ReflectionClass($class);
    }

    return $this->cache[$name];
    }
    }

    @@ -86,10 +92,12 @@ public function getReflection(object $class): ReflectionClass

    //********************** ReflectionClass String ******************************

    $name = get_class($class);

    $start = microtime(true);

    for ($i = 0; $i < TESTS; $i++) {
    $ref = new ReflectionClass(Foo::class);
    $ref = new ReflectionClass($name);
    }

    $ReflectionClassString = microtime(true) - $start;
    @@ -100,11 +108,15 @@ public function getReflection(object $class): ReflectionClass

    class CacheObjectString
    {
    private array $cache = [];
    private $cache = [];

    public function getReflection(string $class): ReflectionClass
    public function getReflection($class)
    {
    return $this->cache[$class] ??= new ReflectionClass($class);
    if (empty($this->cache[$class])) {
    $this->cache[$class] = new ReflectionClass($class);
    }

    return $this->cache[$class];
    }
    }

    @@ -113,7 +125,7 @@ public function getReflection(string $class): ReflectionClass
    $start = microtime(true);

    for ($i = 0; $i < TESTS; $i++) {
    $ref = $cache->getReflection(Foo::class);
    $ref = $cache->getReflection($name);
    }

    $ReflectionClassStringCached = microtime(true) - $start;
  3. eusonlito revised this gist Oct 6, 2023. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions benchmark.php
    Original file line number Diff line number Diff line change
    @@ -82,7 +82,7 @@ public function getReflection(object $class): ReflectionClass
    $ReflectionClassObjectCached = microtime(true) - $start;

    echo "ReflectionClass Object Cached ".sprintf('%0.4f', $ReflectionClassObjectCached)." seconds";
    echo " (".percent($ReflectionClassObject, $ReflectionClassObjectCached)." fastest)\n";
    echo " (".percent($ReflectionClassObject, $ReflectionClassObjectCached)." faster)\n";

    //********************** ReflectionClass String ******************************

    @@ -119,6 +119,6 @@ public function getReflection(string $class): ReflectionClass
    $ReflectionClassStringCached = microtime(true) - $start;

    echo "ReflectionClass String Cached ".sprintf('%0.4f', $ReflectionClassStringCached)." seconds";
    echo " (".percent($ReflectionClassString, $ReflectionClassStringCached)." fastest)\n";
    echo " (".percent($ReflectionClassString, $ReflectionClassStringCached)." faster)\n";

    echo "\n";
    4 changes: 2 additions & 2 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,9 @@ Testing with 1,000,000 iterations

    ReflectionFunction 0.05292 seconds
    ReflectionClass Object 0.04783 seconds
    ReflectionClass Object Cached 0.03557 seconds (26% fastest)
    ReflectionClass Object Cached 0.03557 seconds (26% faster)
    ReflectionClass String 0.05029 seconds
    ReflectionClass String Cached 0.03031 seconds (40% fastest)
    ReflectionClass String Cached 0.03031 seconds (40% faster)

    ------------------------------

  4. eusonlito revised this gist Oct 6, 2023. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion benchmark.php
    Original file line number Diff line number Diff line change
    @@ -118,7 +118,7 @@ public function getReflection(string $class): ReflectionClass

    $ReflectionClassStringCached = microtime(true) - $start;

    echo "ReflectionClass String Cached String ".sprintf('%0.4f', $ReflectionClassStringCached)." seconds";
    echo "ReflectionClass String Cached ".sprintf('%0.4f', $ReflectionClassStringCached)." seconds";
    echo " (".percent($ReflectionClassString, $ReflectionClassStringCached)." fastest)\n";

    echo "\n";
    2 changes: 1 addition & 1 deletion results.txt
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ 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)
    ReflectionClass String Cached 0.03031 seconds (40% fastest)

    ------------------------------

  5. eusonlito revised this gist Oct 6, 2023. No changes.
  6. eusonlito revised this gist Oct 6, 2023. 1 changed file with 14 additions and 9 deletions.
    23 changes: 14 additions & 9 deletions benchmark.php
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ function percent(float $old, float $new): string

    //********************** ReflectionFunction ******************************

    $func = function($a, $b, $c) {};
    $func = function(stdClass $a, Closure $b, float $c): void {};

    $start = microtime(true);

    @@ -33,13 +33,18 @@ function percent(float $old, float $new): string

    class Foo
    {
    public $a;
    protected $b;
    private $c;
    public stdClass $a;
    protected Closure $b;
    private float $c;

    public function foo($a,$b,$c) {}
    protected function bar($a,$b,$c) {}
    private function baz($a,$b,$c) {}
    public function foo(stdClass $a, Closure $b, float $c): void
    {}

    protected function bar(stdClass $a, Closure $b, float $c): void
    {}

    private function baz(stdClass $a, Closure $b, float $c): void
    {}
    }

    $class = new Foo();
    @@ -60,7 +65,7 @@ class CacheObject
    {
    private array $cache = [];

    public function getReflection(object $class)
    public function getReflection(object $class): ReflectionClass
    {
    return $this->cache[$class::class] ??= new ReflectionClass($class);
    }
    @@ -97,7 +102,7 @@ class CacheObjectString
    {
    private array $cache = [];

    public function getReflection(string $class)
    public function getReflection(string $class): ReflectionClass
    {
    return $this->cache[$class] ??= new ReflectionClass($class);
    }
  7. eusonlito created this gist Oct 6, 2023.
    119 changes: 119 additions & 0 deletions benchmark.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,119 @@
    <?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";
    14 changes: 14 additions & 0 deletions results.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    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