- microtime: documentation ( source)
- round: documentation ( source)
<?php
final class IndexRange
{
/**
* @param 0|positive-int $start
* @param 0|positive-int $end
*/
public function __construct(public int $start, public int $end)
{
}
}
function acceptOne($a) {};
function acceptTwo($a, $b) {};
$t = microtime(true);
for ($i = 0; $i < 100_000; $i++) {
$start = $i;
$end = $i;
acceptTwo($start, $end);
}
echo '2 vars: ' . round((microtime(true) - $t) * 1000, 2) . " ms\n";
$t = microtime(true);
for ($i = 0; $i < 100_000; $i++) {
$rangeArr = [$i, $i];
acceptOne($rangeArr);
}
echo 'range arr: ' . round((microtime(true) - $t) * 1000, 2) . " ms\n";
$t = microtime(true);
for ($i = 0; $i < 100_000; $i++) {
$rangeObj = new IndexRange($i, $i);
acceptOne($rangeObj);
}
echo 'range obj: ' . round((microtime(true) - $t) * 1000, 2) . " ms\n";