Last active
August 29, 2015 14:22
-
-
Save wimvds/a6708a67b362ef4a4121 to your computer and use it in GitHub Desktop.
Reflection vs real object instances performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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