|
4 | 4 |
|
5 | 5 | class Benchmark |
6 | 6 | { |
| 7 | + |
7 | 8 | /** |
8 | 9 | * @param $iterations |
9 | 10 | * @param callable $callback |
10 | 11 | * |
11 | 12 | * @return BenchmarkResult |
| 13 | + * |
| 14 | + * @deprecated use task |
12 | 15 | */ |
13 | 16 | public static function test($iterations, callable $callback) |
14 | 17 | { |
15 | | - $i = 0; |
16 | | - $memory = \memory_get_usage(); |
17 | | - $time = \microtime(true); |
18 | | - while ($i < $iterations) |
| 18 | + return self::task($iterations, $callback); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @param $iterations |
| 23 | + * @param callable $callback |
| 24 | + * |
| 25 | + * @return BenchmarkResult |
| 26 | + */ |
| 27 | + public static function task($iterations, callable $callback) |
| 28 | + { |
| 29 | + set_time_limit(0); |
| 30 | + |
| 31 | + $iterator = 0; |
| 32 | + $memory = \memory_get_usage(); |
| 33 | + $time = \microtime(true); |
| 34 | + |
| 35 | + while ($iterator < $iterations) |
19 | 36 | { |
20 | | - $callback(); |
21 | | - $i++; |
| 37 | + $callback($iterator++); |
22 | 38 | } |
23 | | - $stopTime = \microtime(true); |
24 | | - $stopMemory = \memory_get_usage(); |
| 39 | + |
| 40 | + $stopTime = \microtime(true); |
| 41 | + $stopMemory = \memory_get_usage(); |
25 | 42 | $stopMemoryReal = \memory_get_usage(true); |
26 | 43 | $stopMemoryPeak = \memory_get_peak_usage(); |
27 | 44 |
|
28 | | - return new BenchmarkResult([ |
29 | | - 'start' => $time, |
30 | | - 'stop' => $stopTime, |
31 | | - 'execution' => $stopTime - $time, |
32 | | - ], [ |
33 | | - 'usage' => $stopMemory - $memory, |
34 | | - 'realUsage' => $stopMemoryReal, |
35 | | - 'peakUsage' => $stopMemoryPeak, |
36 | | - ]); |
| 45 | + return new BenchmarkResult( |
| 46 | + [ |
| 47 | + 'start' => $time, |
| 48 | + 'stop' => $stopTime, |
| 49 | + 'execution' => $stopTime - $time, |
| 50 | + ], |
| 51 | + [ |
| 52 | + 'usage' => $stopMemory - $memory, |
| 53 | + 'realUsage' => $stopMemoryReal, |
| 54 | + 'peakUsage' => $stopMemoryPeak, |
| 55 | + ] |
| 56 | + ); |
37 | 57 | } |
| 58 | + |
38 | 59 | } |
0 commit comments