Skip to content

Commit

Permalink
[10.x] Add ability to measure a single callable and get result (#48077)
Browse files Browse the repository at this point in the history
* Add ability to measure a single callable and get result

* rename method

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
timacdonald and taylorotwell authored Aug 17, 2023
1 parent d3a5f3e commit e766200
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Illuminate/Support/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@ public static function measure(Closure|array $benchmarkables, int $iterations =
);
}

/**
* Measure a callable once and return the duration and result.
*
* @template TReturn of mixed
*
* @param (callable(): TReturn) $callback
* @return array{0: TReturn, 1: float}
*/
public static function value(callable $callback): array
{
gc_collect_cycles();

$start = hrtime(true);

$result = $callback();

return [$result, (hrtime(true) - $start) / 1000000];
}

/**
* Measure a callable or array of callables over the given number of iterations, then dump and die.
*
Expand Down

0 comments on commit e766200

Please sign in to comment.