Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Reflection speed #33

@sagikazarmark

Description

@sagikazarmark

I am opening this issue just to make sure noone ever tries to do anything bad with the reflection part of generation.

I did some benchmarking as I liked the built-in filtering of reflection properties than filtering the array of them. Turned out it is much slower.

Benchmark (using athletic):

<?php

class Example extends \Athletic\AthleticEvent
{
    protected $class;

    public function setUp()
    {
        $this->class = new LongExampleWithValues;
    }

    /**
     * @iterations 1000
     */
    public function testBuiltInFiltering()
    {
        $reflection = new \ReflectionClass($this->class);

        $reflectionProperties = array_diff(
            $reflection->getProperties(\ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED),
            $reflection->getProperties(\ReflectionProperty::IS_STATIC)
        );
    }

    /**
     * @iterations 1000
     */
    public function testArrayFiltering()
    {
        $reflection = new \ReflectionClass($this->class);

        $reflectionProperties = array_filter(
            $reflection->getProperties(),
            function (\ReflectionProperty $property) {
                return ($property->isPublic() || $property->isProtected()) && ! $property->isStatic();
            }
        );
    }
}

Test class:

<?php

class LongExampleWithValues
{
    private $a = 'a';
    protected $b = null;
    public $c = 1;
    static $d = true;

    public $e = -1;
    public $f = 'F';
    public $g = 1.0;
    public $h = -1.0;
    public $i = PHP_INT_MAX;
    public $j;
}

Results:

Example
    Method Name            Iterations    Average Time      Ops/second
    --------------------  ------------  --------------    -------------
    testBuiltInFiltering: [1,000     ] [0.0000722653866] [13,837.88349]
    testArrayFiltering  : [1,000     ] [0.0000248982906] [40,163.39976]

@Ocramius you are free to close this, just wanted to share some experience.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions