Hi! Could we please enable some services and cookies to improve your experience and our website?

PHPize Online / SQLize Online  /  SQLtest Online

A A A
Login    Share code      Blog   FAQ

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

Copy Format Clear

Stuck with a problem? Got Error? Ask AI support!

Copy Clear
Copy Format Clear
<?php // change the amount of iterations function benchmark($callback, $iterations = 100000) { $start = microtime(true); for ($i = 0; $i < $iterations; $i++) { $callback(); } return microtime(true) - $start; } $array1 = range(1, 50); $array2 = range(51, 100); // Spread operator $spreadTime = benchmark(function () use ($array1, $array2) { $result = [...$array1, ...$array2]; }); // array_merge $mergeTime = benchmark(function () use ($array1, $array2) { $result = array_merge($array1, $array2); }); echo "Spread operator: {$spreadTime} seconds\n"; echo "array_merge: {$mergeTime} seconds\n"; $diff = abs($spreadTime - $mergeTime); $percentage = ($diff / min($spreadTime, $mergeTime)) * 100; if ($spreadTime > $mergeTime) { echo "array_merge was faster by {$percentage}%\n"; } else { echo "Spread operator was faster by {$percentage}%\n"; }
Copy Clear