Skip to content

Instantly share code, notes, and snippets.

@wimvds
Last active August 29, 2015 14:22
Show Gist options
  • Save wimvds/a6708a67b362ef4a4121 to your computer and use it in GitHub Desktop.
Save wimvds/a6708a67b362ef4a4121 to your computer and use it in GitHub Desktop.
Reflection vs real object instances performance
<?php
define('NUM_TESTS', 10);
header('Content-type: text/plain');
interface FooInterface
{
}
class Foo implements FooInterface
{
public $a;
protected $b;
private $c;
}
class Bar
{
public $a;
protected $b;
private $c;
}
class Baz extends Foo
{
}
for ($i=0; $i<NUM_TESTS; $i++)
{
$start = microtime(true);
$ref = new ReflectionClass('Foo');
$check = $ref->implementsInterface('FooInterface');
$end = microtime(true);
echo "ReflectionClass Foo # $i: $check " . number_format(1000000*($end-$start), 3) . " µsec\n";
}
for ($i=0; $i<NUM_TESTS; $i++)
{
$start = microtime(true);
$ref = new ReflectionClass('Bar');
$check = $ref->implementsInterface('FooInterface');
$end = microtime(true);
echo "ReflectionClass Bar # $i: $check " . number_format(1000000*($end-$start), 3) . " µsec\n";
}
for ($i=0; $i<NUM_TESTS; $i++)
{
$start = microtime(true);
$ref = new ReflectionClass('Baz');
$check = $ref->implementsInterface('FooInterface');
$end = microtime(true);
echo "ReflectionClass Baz # $i: $check " . number_format(1000000*($end-$start), 3) . " µsec\n";
}
for ($i=0; $i<NUM_TESTS; $i++)
{
$start = microtime(true);
$ref = new Foo();
$check = $ref instanceof FooInterface;
$end = microtime(true);
echo "Real Foo # $i: $check " . number_format(1000000*($end-$start), 3) . " µsec\n";
}
for ($i=0; $i<NUM_TESTS; $i++)
{
$start = microtime(true);
$ref = new Bar();
$check = $ref instanceof FooInterface;
$end = microtime(true);
echo "Real Bar # $i: $check " . number_format(1000000*($end-$start), 3) . " µsec\n";
}
for ($i=0; $i<NUM_TESTS; $i++)
{
$start = microtime(true);
$ref = new Baz();
$check = $ref instanceof FooInterface;
$end = microtime(true);
echo "Real Baz # $i: $check " . number_format(1000000*($end-$start), 3) . " µsec\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment