This article will share with you three PHP array merging methods: array_merge, array_push and element append method, and compare their performance and memory consumption.
To implement array merging in PHP, there are two commonly used methods:
##⒈ Code implementation
/** 用 PHP 实现将一个二维数组合并为一个一维数组 */ // 数组的长度 $len = $argv[1]; $start = 0; $arr = []; // 数组初始化 while ($start < $len) { $arr[$start] = range(1, $len); $start ++; } // 采用 array_merge 的方式合并数组 function merge1 (array $input) { $results = []; foreach ($input as $item) { $results = array_merge($results, $item); } return $results; } // 采用 array_push 的方式合并数组 function merge2 (array $input) { $results = []; foreach ($input as $item) { foreach ($item as $v) { array_push($results, $v); } } return $results; } // 采用元素追加的方式合并数组 function merge3(array $input) { $results = []; foreach ($input as $item) { foreach ($item as $v) { $results [] = $v; } } return $results; } echo microtime(), PHP_EOL; $res = merge3($arr); echo microtime(), PHP_EOL; echo memory_get_usage(), PHP_EOL; echo memory_get_peak_usage(), PHP_EOL;
⒉ Comparison of running results
array_merge | array_push | Element append | ||||
Running time(㎲) | Memory consumption(B) | Running time(㎲) | Memory consumption(B) | Running time(㎲) | ||
432,440 | 20 | 433,200 | 25 | 433,200 | 19 | |
764,704 | 322 | 629,736 | 141 | 629,736 | 102 | |
27,434,944 | 897,422 | 19,042,416 | 11,294 | 19,042,416 | 8,182 | |
3,479,599,136 | 904,476,129 | 2,405,853,392 | 1,266,203 | 2,405,853,392 | 1,022,328 |
⒊ Cause Analysis
, each time it is called, new memory needs to be allocated for the result array of array_merge
Space (PHP uses a copy-on-write mechanism, and array_merge
does not write parameters, so there is no need to reallocate memory for input parameters each time it is called). As the length of the array increases, the number of calls to array_merge
will also increase, and the number of reallocations of memory will also increase. At the same time, the length of the result array will continue to increase, and the memory space that needs to be allocated each time will also increase. Increase, which requires consuming a lot of memory. At the same time, frequent allocation/destruction of memory leads to PHP performance degradation.
, you only need to allocate memory to the result array once, and then elements will be appended to the result array. The underlying implementation of PHP array is hashtable
, and hashtable
will be allocated 8 lengths of bucket
during initialization, each bucket
Stores an array element. When the existing bucket
is used up, if you continue to append elements to the array, PHP will double the number of existing bucket
, so use array_push
When appending elements to the result array, not every operation requires allocating a new bucket
, which reduces the number of memory allocations, speeds up the program running time, and improves PHP performance.
. The only difference is that there will be a function call when using array_push
, and the element appending The method does not require function calls, so the program will run more lightweight.
The above is the detailed content of PHP array merging method: array_merge VS array_push VS element append. For more information, please follow other related articles on the PHP Chinese website!