Home > Article > Backend Development > Practical solution for handling intersection and union of large-scale PHP arrays
A practical solution for processing large-scale PHP array intersections and unions
Introduction
When working with large data, it is often necessary to perform array intersection and union operations. But for large arrays with millions or billions of elements, the default PHP functions may be inefficient or suffer from memory issues. This article will introduce several practical solutions to significantly improve performance when working with large arrays.
Method 1: Using Hash Table
Code example:
$arr1 = range(1, 1000000); $arr2 = range(500001, 1500000); $hash = array_flip($arr1); $intersection = array_keys(array_intersect_key($hash, $arr2));
Method 2: Using the Hashes.php library
Intersect()
method. For union operations, use the Union()
method. Code example:
use Hashes\Hash; $map = new Hash(); foreach ($arr1 as $val) { $map->add($val); } $intersection = $map->intersect($arr2); $union = $map->union($arr2);
Method 3: Use bitwise operation
Code Example:
function bitInterset($arr1, $arr2) { $max = max(max($arr1), max($arr2)); $bitSize = 32; // 如果 max > (2^32 - 1),可以调整 bitSize $bitmap1 = array_fill(0, $bitSize, 0); $bitmap2 = array_fill(0, $bitSize, 0); foreach ($arr1 as $num) { $bitmap1[$num >> 5] |= (1 << ($num & 31)); } foreach ($arr2 as $num) { $bitmap2[$num >> 5] |= (1 << ($num & 31)); } $intersection = []; for ($i = 0; $i < $bitSize; $i++) { $mask = $bitmap1[$i] & $bitmap2[$i]; for ($j = 0; $j < 32; $j++) { if (($mask >> $j) & 1) { $intersection[] = ($i << 5) | $j; } } } return $intersection; }
Practical Case
Let us consider an array containing one hundred million elements , we want to find its intersection and union with another array containing five million elements.
Using method 1 (hash table):
Using the Hashes.php library (Method 2):
Use bitwise operation (Method 3):
As you can see, the bitwise operation takes 1.2 seconds to process such a large scale Provides the best performance when using arrays.
The above is the detailed content of Practical solution for handling intersection and union of large-scale PHP arrays. For more information, please follow other related articles on the PHP Chinese website!