@ 2021-07-25T21:17:45Z <?php
declare(strict_types = 1);
/** @noinspection AutoloadingIssuesInspection */
class BenchmarkCase
{
public static $maxNameLength;
public $callable;
public $iterations = 0;
public $name;
public $timing = 0;
public static function create(string $name, callable $callable): self
{
$self = new self;
$self->name = $name;
$self->callable = $callable;
self::$maxNameLength = max(self::$maxNameLength, strlen($name));
return $self;
}
public static function printResults(float $initialMs, array $cases): void
{
$durationMs = microtime(true) - $initialMs;
printf("\r");
usort($cases, static function (BenchmarkCase $a, BenchmarkCase $b) {
return $a->timing <=> $b->timing;
});
$caseFirst = $cases[0]->timing;
/** @var BenchmarkCase $case */
foreach ($cases as $case) {
printf("Case %s duration of %.6fs (avg. each %.8fs) +%.2f%%\n",
str_pad($case->name, self::$maxNameLength, ' '),
$case->timing,
bcdiv((string) $case->timing, (string) $case->iterations, 8),
(100 / $caseFirst * $case->timing) - 100);
}
printf("\nTotal duration: %.6fs",
$durationMs);
}
}
set_error_handler(static function ($severity, $message) {
throw new ErrorException($message, 0, $severity);
});
$cases = [
BenchmarkCase::create('array_merge() inside looping', static function () {
$arr = [];
for ($i = 0; $i < 100; $i++) {
$arr = array_merge($arr, [ $i, $i ]);
}
return $arr;
}),
BenchmarkCase::create('array_merge() after looping', static function () {
$arrArr = [];
for ($i = 0; $i < 100; $i++) {
$arrArr[] = [ $i, $i ];
}
return array_merge(... $arrArr);
}),
BenchmarkCase::create('array_push() inside looping', static function () {
$arr = [];
for ($i = 0; $i < 100; $i++) {
array_push($arr, $i, $i);
}
return $arr;
}),
];
const BENCHMARK_DURATION_MS = 1500;
const CYCLES_ITERATION = 100;
$initialMs = microtime(true);
$stopTiming = $initialMs + BENCHMARK_DURATION_MS / 1000;
while (($currentTiming = microtime(true)) < $stopTiming) {
/** @noinspection NonSecureShuffleUsageInspection */
shuffle($cases);
/** @var BenchmarkCase $case */
foreach ($cases as $case) {
$startMs = microtime(true);
for ($j = 0; $j < CYCLES_ITERATION; $j++) {
call_user_func($case->callable);
}
$case->iterations += CYCLES_ITERATION;
$case->timing += microtime(true) - $startMs;
}
}
BenchmarkCase::printResults($initialMs, $cases);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.0.8 0.018 1.491 16.99 8.0.7 0.020 1.501 17.00 8.0.6 0.010 1.499 16.91 8.0.5 0.013 1.489 16.97 8.0.3 0.016 1.491 17.02 8.0.2 0.003 1.501 17.10 8.0.1 0.010 1.500 17.06 8.0.0 0.016 1.508 17.17 7.4.21 0.013 1.488 16.48 7.4.20 0.020 1.490 16.68 7.4.19 0.010 1.501 16.84 7.4.18 0.019 1.505 16.64 7.4.16 0.023 1.493 16.64 7.4.15 0.023 1.487 16.57 7.4.14 0.022 1.503 16.68 7.4.13 0.026 1.502 16.46 7.4.12 0.013 1.506 16.69 7.4.11 0.013 1.506 16.72 7.4.10 0.033 1.476 16.52 7.4.9 0.010 1.503 16.55 7.4.8 0.019 1.497 16.50 7.4.7 0.020 1.494 16.67 7.4.6 0.013 1.505 16.60 7.4.5 0.010 1.506 16.49 7.4.4 0.026 1.494 16.47 7.4.3 0.017 1.498 16.63 7.4.2 0.013 1.509 16.46 7.4.1 0.016 1.488 16.72 7.4.0 0.029 1.481 16.60 7.3.29 0.020 1.506 16.39 7.3.28 0.027 1.495 16.45 7.3.27 0.023 1.501 16.43 7.3.26 0.016 1.508 16.36 7.3.25 0.026 1.502 16.44 7.3.24 0.026 1.504 16.41 7.3.23 0.023 1.494 16.46 7.3.22 0.026 1.490 16.43 7.3.21 0.022 1.504 16.38 7.3.20 0.010 1.506 16.30 7.3.19 0.026 1.504 16.43 7.3.18 0.033 1.494 16.41 7.3.17 0.016 1.506 16.39 7.3.16 0.020 1.507 16.35 7.3.15 0.020 1.507 16.43 7.3.14 0.019 1.515 16.40 7.3.13 0.017 1.517 16.40 7.3.12 0.026 1.498 16.39 7.3.11 0.017 1.505 16.39 7.3.10 0.020 1.507 16.34 7.3.9 0.030 1.492 16.57 7.3.8 0.017 1.516 16.37 7.3.7 0.020 1.509 16.43 7.3.6 0.033 1.491 16.52 7.3.5 0.023 1.493 16.55 7.3.4 0.029 1.479 16.53 7.3.3 0.026 1.495 16.58 7.3.2 0.013 1.511 16.56 7.3.1 0.026 1.495 16.49 7.3.0 0.023 1.498 16.57 7.1.33 0.022 1.414 15.74
preferences:dark mode live preview
36.58 ms | 401 KiB | 5 Q