<?php
function benchmark(callable $fn): array {
$start = hrtime(true);
$result = $fn();
$elapsedNs = hrtime(true) - $start;
return [
'result' => $result,
'ms' => $elapsedNs / 1_000_000,
];
}
$bench = benchmark(function () {
return array_sum(range(1, 1_000_000));
});
printf("Result: %d took %.2f ms\n", $bench['result'], $bench['ms']);
// Result: 500000500000 took 12.34 ms
Create a free account and build your private vault. Share publicly whenever you want.