From 1addcc5018a3d146208d51a80f3a4216a89128d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=B0=D0=B1=D0=B8=D1=87=D0=B5=D0=B2=20=D0=9C=D0=B0?= =?UTF-8?q?=D0=BA=D1=81=D0=B8=D0=BC?= Date: Thu, 9 Mar 2017 14:17:08 +0300 Subject: [PATCH] Update Benchmark.php --- src/MicroBenchmark/Benchmark.php | 55 ++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/src/MicroBenchmark/Benchmark.php b/src/MicroBenchmark/Benchmark.php index 7cacd52..2624065 100644 --- a/src/MicroBenchmark/Benchmark.php +++ b/src/MicroBenchmark/Benchmark.php @@ -4,35 +4,56 @@ class Benchmark { + /** * @param $iterations * @param callable $callback * * @return BenchmarkResult + * + * @deprecated use task */ public static function test($iterations, callable $callback) { - $i = 0; - $memory = \memory_get_usage(); - $time = \microtime(true); - while ($i < $iterations) + return self::task($iterations, $callback); + } + + /** + * @param $iterations + * @param callable $callback + * + * @return BenchmarkResult + */ + public static function task($iterations, callable $callback) + { + set_time_limit(0); + + $iterator = 0; + $memory = \memory_get_usage(); + $time = \microtime(true); + + while ($iterator < $iterations) { - $callback(); - $i++; + $callback($iterator++); } - $stopTime = \microtime(true); - $stopMemory = \memory_get_usage(); + + $stopTime = \microtime(true); + $stopMemory = \memory_get_usage(); $stopMemoryReal = \memory_get_usage(true); $stopMemoryPeak = \memory_get_peak_usage(); - return new BenchmarkResult([ - 'start' => $time, - 'stop' => $stopTime, - 'execution' => $stopTime - $time, - ], [ - 'usage' => $stopMemory - $memory, - 'realUsage' => $stopMemoryReal, - 'peakUsage' => $stopMemoryPeak, - ]); + return new BenchmarkResult( + [ + 'start' => $time, + 'stop' => $stopTime, + 'execution' => $stopTime - $time, + ], + [ + 'usage' => $stopMemory - $memory, + 'realUsage' => $stopMemoryReal, + 'peakUsage' => $stopMemoryPeak, + ] + ); } + }