Skip to content
This repository was archived by the owner on Aug 13, 2023. It is now read-only.

Commit 1addcc5

Browse files
authored
Update Benchmark.php
1 parent bc5452f commit 1addcc5

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

src/MicroBenchmark/Benchmark.php

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,56 @@
44

55
class Benchmark
66
{
7+
78
/**
89
* @param $iterations
910
* @param callable $callback
1011
*
1112
* @return BenchmarkResult
13+
*
14+
* @deprecated use task
1215
*/
1316
public static function test($iterations, callable $callback)
1417
{
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)
1936
{
20-
$callback();
21-
$i++;
37+
$callback($iterator++);
2238
}
23-
$stopTime = \microtime(true);
24-
$stopMemory = \memory_get_usage();
39+
40+
$stopTime = \microtime(true);
41+
$stopMemory = \memory_get_usage();
2542
$stopMemoryReal = \memory_get_usage(true);
2643
$stopMemoryPeak = \memory_get_peak_usage();
2744

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+
);
3757
}
58+
3859
}

0 commit comments

Comments
 (0)