Created
September 29, 2014 11:20
-
-
Save lozcalver/bd3d36302d69d8756734 to your computer and use it in GitHub Desktop.
ReflectionClass vs new Class
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 | |
// https://gist.github.com/mindplay-dk/3359812 | |
define('NUM_TESTS', 10); | |
header('Content-type: text/plain'); | |
class Foo | |
{ | |
public $a; | |
public function __construct($arg1 = null) { | |
$this->a = $arg1; | |
} | |
} | |
for ($i=0; $i<NUM_TESTS; $i++) { | |
$start = microtime(true); | |
$reflector = new ReflectionClass('Foo'); | |
$inst = $reflector->newInstanceArgs(array('foo')); | |
$end = microtime(true); | |
echo "ReflectionClass with arg # $i: " . number_format(1000000*($end-$start), 3) . " µsec\n"; | |
} | |
for ($i=0; $i<NUM_TESTS; $i++) { | |
$start = microtime(true); | |
$ref = new Foo('foo'); | |
$end = microtime(true); | |
echo "new Class with arg # $i: " . number_format(1000000*($end-$start), 3) . " µsec\n"; | |
} | |
for ($i=0; $i<NUM_TESTS; $i++) { | |
$start = microtime(true); | |
$reflector = new ReflectionClass('Foo'); | |
$end = microtime(true); | |
echo "ReflectionClass without arg # $i: " . number_format(1000000*($end-$start), 3) . " µsec\n"; | |
} | |
for ($i=0; $i<NUM_TESTS; $i++) { | |
$start = microtime(true); | |
$ref = new Foo(); | |
$end = microtime(true); | |
echo "new Class without arg # $i: " . 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
Swapping the two approaches around to check if there’s any caching going on (it seems there is):