Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function customMerge(array ...$arrays) {
- $all = [];
- foreach ($arrays as $array) {
- foreach ($array as $key => $value) {
- $all[$key] = $value;
- }
- }
- return $all;
- }
- $first = [
- 'key' => 'value',
- 111 => 'chuck norris',
- ];
- $second = [
- 666 => 'hi',
- 333 => 'hello world',
- ];
- $third = [
- 'key' => 'another value',
- 999 => 'chuck norris',
- ];
- foreach (['first', 'second', 'third'] as $name) {
- for ($i=0; $i < 700; $i++) {
- $ai = rand(1,999);
- ${$name}[$ai] = rand(10000,99999);
- }
- }
- $iterations = 50000;
- $a = microtime(true);
- for ($i = 0; $i < $iterations; $i++) {
- array_replace($first, $second, $third);
- }
- printf('array_replace (%dx): %.2fs%s', $iterations, microtime(true) - $a, PHP_EOL);
- $b = microtime(true);
- for ($i = 0; $i < $iterations; $i++) {
- customMerge($first, $second, $third);
- }
- printf('customMerge (%dx): %.2fs%s', $iterations, microtime(true) - $b, PHP_EOL);
- // Final Output:
- // array_replace (50000x): 1.15s
- // customMerge (50000x): 17.43s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement