Skip to content

Instantly share code, notes, and snippets.

@kinglozzer
Created September 29, 2014 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinglozzer/bd3d36302d69d8756734 to your computer and use it in GitHub Desktop.
Save kinglozzer/bd3d36302d69d8756734 to your computer and use it in GitHub Desktop.
ReflectionClass vs new Class
<?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";
}
@kinglozzer
Copy link
Author

ReflectionClass with arg # 0: 12.875 µsec
ReflectionClass with arg # 1: 5.007 µsec
ReflectionClass with arg # 2: 3.099 µsec
ReflectionClass with arg # 3: 2.861 µsec
ReflectionClass with arg # 4: 1.907 µsec
ReflectionClass with arg # 5: 2.861 µsec
ReflectionClass with arg # 6: 1.907 µsec
ReflectionClass with arg # 7: 2.146 µsec
ReflectionClass with arg # 8: 2.146 µsec
ReflectionClass with arg # 9: 3.099 µsec
new Class with arg # 0: 0.954 µsec
new Class with arg # 1: 1.192 µsec
new Class with arg # 2: 0.954 µsec
new Class with arg # 3: 0.954 µsec
new Class with arg # 4: 0.000 µsec
new Class with arg # 5: 0.954 µsec
new Class with arg # 6: 0.954 µsec
new Class with arg # 7: 0.954 µsec
new Class with arg # 8: 0.954 µsec
new Class with arg # 9: 0.954 µsec
ReflectionClass without arg # 0: 2.146 µsec
ReflectionClass without arg # 1: 0.954 µsec
ReflectionClass without arg # 2: 0.954 µsec
ReflectionClass without arg # 3: 0.954 µsec
ReflectionClass without arg # 4: 1.192 µsec
ReflectionClass without arg # 5: 0.954 µsec
ReflectionClass without arg # 6: 0.954 µsec
ReflectionClass without arg # 7: 0.954 µsec
ReflectionClass without arg # 8: 0.954 µsec
ReflectionClass without arg # 9: 0.954 µsec
new Class without arg # 0: 1.907 µsec
new Class without arg # 1: 0.954 µsec
new Class without arg # 2: 0.954 µsec
new Class without arg # 3: 0.000 µsec
new Class without arg # 4: 0.954 µsec
new Class without arg # 5: 0.954 µsec
new Class without arg # 6: 0.954 µsec
new Class without arg # 7: 0.954 µsec
new Class without arg # 8: 1.192 µsec
new Class without arg # 9: 0.000 µsec

Swapping the two approaches around to check if there’s any caching going on (it seems there is):

new Class with arg # 0: 6.199 µsec
new Class with arg # 1: 1.907 µsec
new Class with arg # 2: 1.907 µsec
new Class with arg # 3: 0.954 µsec
new Class with arg # 4: 1.192 µsec
new Class with arg # 5: 0.954 µsec
new Class with arg # 6: 0.954 µsec
new Class with arg # 7: 0.954 µsec
new Class with arg # 8: 0.954 µsec
new Class with arg # 9: 0.954 µsec
ReflectionClass with arg # 0: 7.868 µsec
ReflectionClass with arg # 1: 4.053 µsec
ReflectionClass with arg # 2: 3.099 µsec
ReflectionClass with arg # 3: 3.099 µsec
ReflectionClass with arg # 4: 1.907 µsec
ReflectionClass with arg # 5: 2.861 µsec
ReflectionClass with arg # 6: 1.907 µsec
ReflectionClass with arg # 7: 3.099 µsec
ReflectionClass with arg # 8: 2.146 µsec
ReflectionClass with arg # 9: 3.099 µsec
new Class without arg # 0: 0.954 µsec
new Class without arg # 1: 0.954 µsec
new Class without arg # 2: 1.192 µsec
new Class without arg # 3: 0.954 µsec
new Class without arg # 4: 0.000 µsec
new Class without arg # 5: 0.954 µsec
new Class without arg # 6: 0.954 µsec
new Class without arg # 7: 1.192 µsec
new Class without arg # 8: 0.954 µsec
new Class without arg # 9: 1.192 µsec
ReflectionClass without arg # 0: 0.954 µsec
ReflectionClass without arg # 1: 0.954 µsec
ReflectionClass without arg # 2: 0.954 µsec
ReflectionClass without arg # 3: 0.954 µsec
ReflectionClass without arg # 4: 0.954 µsec
ReflectionClass without arg # 5: 0.954 µsec
ReflectionClass without arg # 6: 1.192 µsec
ReflectionClass without arg # 7: 0.954 µsec
ReflectionClass without arg # 8: 0.954 µsec
ReflectionClass without arg # 9: 0.954 µsec

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment