hrtime(); $result = $callback(); $end = $this->hrtime(); $duration = $end->subtract($start); return [ 'result' => $result, 'duration' => $duration, ]; } /** * {@inheritDoc} */ public function benchmark(callable $callback, int $iterations = 1): array { if ($iterations < 1) { throw new \InvalidArgumentException('Iterations must be at least 1'); } $durations = []; $totalDuration = Duration::zero(); for ($i = 0; $i < $iterations; $i++) { $start = $this->hrtime(); $callback(); $end = $this->hrtime(); $duration = $end->subtract($start); $durations[] = $duration; $totalDuration = $totalDuration->add($duration); } $averageDuration = Duration::fromNanoseconds( (int) round($totalDuration->toNanoseconds() / $iterations) ); $minDuration = $durations[0]; $maxDuration = $durations[0]; foreach ($durations as $duration) { if ($duration->toNanoseconds() < $minDuration->toNanoseconds()) { $minDuration = $duration; } if ($duration->toNanoseconds() > $maxDuration->toNanoseconds()) { $maxDuration = $duration; } } return [ 'total' => $totalDuration, 'average' => $averageDuration, 'min' => $minDuration, 'max' => $maxDuration, ]; } }