[英]Java data storage optimization options
I've been considering the optimization of two of my latest data storage techniques in Java, and would like to know which is really the most memory-efficient . 我一直在考虑用Java优化我最新的两种数据存储技术,并且想知道哪种内存效率最高 。 Below are descriptions of the two classes.
以下是这两个类的描述。 For the sake of argument, assume they have the same methods for interfacing with the data, which allow the user to get or set the state of any of the bits individually or by range via the following methods:
为了便于论证,假设它们具有与数据接口相同的方法,允许用户通过以下方法单独或按范围获取或设置任何位的状态:
public boolean getBitState(byte bitIndex)
bitIndex
bitIndex
处的位状态 public Clazz setBitState(byte bitIndex, boolean newState)
bitIndex
to newState
and returns the resulting object bitIndex
处的位状态设置为newState
并返回结果对象 public int getStateOfBits(byte startIndex, byte endIndex)
startIndex
and endIndex
as an int
startIndex
和endIndex
之间所有位的状态为int
public Clazz setStateOfBits(byte startIndex, byte endIndex, int newState)
startIndex
and endIndex
to the value provided in newState
. startIndex
和endIndex
之间的所有位的状态设置为newState
提供的值。 newState
has fewer bits than fit, it is made to fit by adding zeros to the left newState
的位数少于fit,则通过在左侧添加零来使其适合 newState
has more bits than fit, the excess bits (on the left side) are cropped newState
位数多于fit,则会裁剪多余的位(在左侧) These are the classes I have made to utilize this interface: 这些是我使用此接口所做的类:
This class uses an int
as a way of storing 32 bits of data through bitwise functions. 该类使用
int
作为通过按位函数存储32位数据的方法。
This class uses an array of 32 boolean
s as its way of storing 32 bits of data through standard array interactions. 该类使用32个
boolean
的数组作为通过标准数组交互存储32位数据的方式。
Use an int
and bitwise functions! 使用
int
和按位函数! Most JVMs will represent an array of boolean
as an array of bytes. 大多数JVM将
boolean
数组表示为字节数组。 java.util.BitSet
uses internally an array of longs to represent it's bits (chunks of 64). java.util.BitSet
内部使用long数组来表示它的位(64块)。
Have you considered using the BitSet
class ? 您是否考虑过使用
BitSet
类 ? It seems like it does everything you need to do, and is probably well optimized, memory-wise. 看起来它可以完成您需要做的所有事情,并且可能在记忆方面得到很好的优化。
Considering your two choices, definitely not an array of booleans. 考虑到你的两个选择,绝对不是一系列布尔。 And array of boolean requires extra memory space for the metadata associated to the datatype.
布尔数组需要额外的内存空间用于与数据类型关联的元数据。 Plus, most JVM will allocate 32 bits of memory for each boolean.
此外,大多数JVM将为每个布尔值分配32位内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.