- microtime: documentation ( source)
<?php
$iterations = 1_000_000;
class Result {
}
benchmark('Result object', fn() => new Result());
benchmark('Exceptions', fn() => throw new \RuntimeException());
function benchmark(string $label, \Closure $closure) {
global $iterations;
$sT = microtime(true);
for ($i = 0; $i < $iterations; $i ++) {
try {
$closure();
} catch (\Exception $e) {
}
}
echo $label . ' : ' . (microtime(true) - $sT) . 's' . PHP_EOL;
}