3v4l.org

run code in 300+ PHP versions simultaneously
<?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);

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).

VersionSystem time (s)User time (s)Memory (MiB)
8.0.80.0181.49116.99
8.0.70.0201.50117.00
8.0.60.0101.49916.91
8.0.50.0131.48916.97
8.0.30.0161.49117.02
8.0.20.0031.50117.10
8.0.10.0101.50017.06
8.0.00.0161.50817.17
7.4.210.0131.48816.48
7.4.200.0201.49016.68
7.4.190.0101.50116.84
7.4.180.0191.50516.64
7.4.160.0231.49316.64
7.4.150.0231.48716.57
7.4.140.0221.50316.68
7.4.130.0261.50216.46
7.4.120.0131.50616.69
7.4.110.0131.50616.72
7.4.100.0331.47616.52
7.4.90.0101.50316.55
7.4.80.0191.49716.50
7.4.70.0201.49416.67
7.4.60.0131.50516.60
7.4.50.0101.50616.49
7.4.40.0261.49416.47
7.4.30.0171.49816.63
7.4.20.0131.50916.46
7.4.10.0161.48816.72
7.4.00.0291.48116.60
7.3.290.0201.50616.39
7.3.280.0271.49516.45
7.3.270.0231.50116.43
7.3.260.0161.50816.36
7.3.250.0261.50216.44
7.3.240.0261.50416.41
7.3.230.0231.49416.46
7.3.220.0261.49016.43
7.3.210.0221.50416.38
7.3.200.0101.50616.30
7.3.190.0261.50416.43
7.3.180.0331.49416.41
7.3.170.0161.50616.39
7.3.160.0201.50716.35
7.3.150.0201.50716.43
7.3.140.0191.51516.40
7.3.130.0171.51716.40
7.3.120.0261.49816.39
7.3.110.0171.50516.39
7.3.100.0201.50716.34
7.3.90.0301.49216.57
7.3.80.0171.51616.37
7.3.70.0201.50916.43
7.3.60.0331.49116.52
7.3.50.0231.49316.55
7.3.40.0291.47916.53
7.3.30.0261.49516.58
7.3.20.0131.51116.56
7.3.10.0261.49516.49
7.3.00.0231.49816.57
7.1.330.0221.41415.74

preferences:
36.58 ms | 401 KiB | 5 Q