代表一组比特的好方法是什么?
我有一组各种开/关切换(成千上万)并需要存储和检索其状态。 天真的实现将是一组布尔值,但我想知道是否有更好的方法(更好的访问速度和/或内存要求)。
我发现这个BitArray的实现,但是它限于32位,这对于这种情况是不够的。
What is a good way to represent a collection of bits?
I have a set of various on/off toggles (thousands of them) and need to store and retrieve their state. The naïve implementation would be an array of booleans, but I'm wondering if there's a better way (better in terms of access speed and/or memory requirements).
I've found this BitArray implementation, but it's limited to 32 bits, which is not enough for this case.
最满意答案
另一个选择是将它们以整数存储在PHP_INT_SIZE*8块中,并使用按位运算符来设置/取消设置它们。
我不能评论这种方法的速度或内存消耗,你可能需要做一些基准测试。
Another option is to store them in blocks of PHP_INT_SIZE*8 in integers and use bitwise operators to set/unset them.
I can't comment on speeds or memory consumption of this method, you may have to do some benchmarking.
更多推荐
发布评论