- var_dump: documentation ( source)
- microtime: documentation ( source)
- str_pad: documentation ( source)
<?php
if (PHP_VERSION !== '7.2.0alpha2') die('Meh');
function test($label = '') {
global $t;
$t2 = microtime(1);
if ($label) echo str_pad($label . ': ', 32, '.', STR_PAD_RIGHT) . ' ' . 1000 * ($t2 - $t) . 'ms' . "\n";
$t = $t2;
}
$c = 1;
$c2 = 10000;
// ---------------------
class Meh {
function hem() { return 1; }
}
class MehBag {
public $meh1, $meh2, $meh3;
function __construct(Meh $meh) {
$this->meh1 = $meh;
$this->meh2 = $meh;
$this->meh3 = $meh;
}
}
$x1 = function (Meh $meh1, Meh $meh2, Meh $meh3) {
return $meh1->hem() + $meh2->hem() + $meh3->hem();
};
$x2 = function (MehBag $bag) {
return $bag->meh1->hem() + $bag->meh2->hem() + $bag->meh3->hem();
};
// ---------------------
test();
for ($i = $c; $i--;) {
for ($j = $c2; $j--;) {
$rc = new \ReflectionFunction($x1);
$rp = $rc->getParameters();
$bag = [
Meh::class => new Meh()
];
$args = [];
foreach ($rp as $p) {
$pc = $p->getClass();
$name = $pc->name ?? null;
if ($name) $args[] = $bag[$name];
}
$result1 = $x1(...$args);
}
}
test('ref');
for ($i = $c; $i--;) {
for ($j = $c2; $j--;) {
$bag = new MehBag(new Meh());
$result2 = $x2($bag);
}
}
test('bag');
var_dump($result1);
var_dump($result2);