- microtime: documentation ( source)
- round: documentation ( source)
<?php
class IndexRange
{
/** @var 0|positive-int */
public int $start;
/** @var 0|positive-int */
public int $end;
/**
* @param 0|positive-int $start
* @param 0|positive-int $end
*/
public function __construct(int $start = null, int $end = null)
{
if (null !== $start) {
$this->start = $start;
}
if (null !== $end) {
$this->end = $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";