我正在寻找一种在 PHP 内存中存储大量布尔值(最多 2.5*10e11)的有效方法。我的第一个想法是创建一个整数数组并在每个整数中存储一个布尔值:
// number of booleans to store
$n = 2.5 * pow(10, 11);
// bits per integer
$bitsPerInt = PHP_INT_SIZE * 8;
// init storage
$storage = array();
for ($i=0; $i<ceil($n/$bitsPerInt); $i++) {
$storage[$i] = 0;
}
// bits in each integer can be accessed using PHP's bitwise operators
但是,这种解决方案的开销仍然太大:在 32 位环境(PHP_INT_SIZE = 4 字节)中存储 10^8 个布尔值(位)需要 3125000 个整数的数组,消耗约 254 MB 内存,而罕见的10^8 个布尔值的数据只需要 ~ 12 MB。
那么在 PHP (5) 中存储大量布尔值的最佳方式是什么?