简体   繁体   English

管理大量布尔状态的最有效方法

[英]Most efficient way to manage large amount of boolean state

I want to manage the boolean state (on/off) of up to 10^18 items. 我想管理最多10 ^ 18项的布尔状态(开/关)。 What is the most memory and computationally efficient way of doing this in Java? 在Java中执行此操作的内存和计算效率最高的方法是什么? I can't create an array of booleans this large as the primitive type of the size of an array is int , and the same goes for the BitSet class. 我无法创建这么大的布尔数组,因为数组大小的基本类型是int ,而BitSet类也是如此。

Eg: 例如:

long numSwitches = Long.MAX_VALUE;
boolean[] switches = new boolean[numSwitches];

gives me a compilation error: Incompatible types, Reguired: int, Found: long 给我一个编译错误: Incompatible types, Reguired: int, Found: long

I honestly don't think something like this is possible without the use of a supercomputer. 老实说,如果不使用超级计算机,我认为这样的事情是不可能的。 10^18 is a very big number, it's actually relatively close to the number of atoms in a single grain of salt! 10 ^ 18是一个非常大的数字,它实际上相对接近单粒盐中的原子数! If each atom is 1 bit, then the total memory will be an upwards of 125 petabytes (125,000 terabytes)! 如果每个原子是1位,那么总内存将高达125 PB(125,000太字节)!

Even if you wish to access a bit sequentially (random access will require you to load it all into memory) via the use of some sort of stream, it would still take a ridiculous amount of time. 即使您希望通过使用某种流顺序访问位(随机访问将要求您将其全部加载到内存中),它仍然需要花费大量时间。

Unless only a few bits compared to the total amount will be enabled at a time (a few billion, which would still require gigabytes of memory), I don't see any plausible way to solve this unfortunately. 除非一次启用只有几位与总量相比(几十亿,这仍然需要几千兆字节的内存),我不认为有任何合理的解决方法。 However, it's still very interesting to think about. 但是,考虑一下它仍然非常有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM