Skip to content

Commit e766200

Browse files
[10.x] Add ability to measure a single callable and get result (#48077)
* Add ability to measure a single callable and get result * rename method --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d3a5f3e commit e766200

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/Illuminate/Support/Benchmark.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ public static function measure(Closure|array $benchmarkables, int $iterations =
3232
);
3333
}
3434

35+
/**
36+
* Measure a callable once and return the duration and result.
37+
*
38+
* @template TReturn of mixed
39+
*
40+
* @param (callable(): TReturn) $callback
41+
* @return array{0: TReturn, 1: float}
42+
*/
43+
public static function value(callable $callback): array
44+
{
45+
gc_collect_cycles();
46+
47+
$start = hrtime(true);
48+
49+
$result = $callback();
50+
51+
return [$result, (hrtime(true) - $start) / 1000000];
52+
}
53+
3554
/**
3655
* Measure a callable or array of callables over the given number of iterations, then dump and die.
3756
*

0 commit comments

Comments
 (0)