- microtime: documentation ( source)
<?php
class Bar {
}
class Foo {
private $bar;
public function __construct(Bar $bar) {
$this->bar = $bar;
}
}
$reflect = new ReflectionClass('foo');
$constructor = $reflect->getConstructor();
$bar = new Bar;
$aBar = [$bar];
$t1 = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$foo = new Foo($bar);
}
$t2 = microtime(true);
echo 'new keyword: ' . ($t2-$t1) . "\n\n";
$t1 = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$foo = $reflect->newInstanceArgs($aBar);
}
$t2 = microtime(true);
echo 'newinstanceargs: ' . ($t2-$t1) . "\n\n";
$t1 = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$foo = $reflect->newInstanceWithoutConstructor();
$constructor->invokeArgs($foo, $aBar);
}
$t2 = microtime(true);
echo 'newinstancewithoutconstructor: ' . ($t2-$t1) . "\n\n";
$t1 = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$foo = new Foo(...$aBar);
}
$t2 = microtime(true);
echo 'new keyword 2: ' . ($t2-$t1) . "\n\n";