I am looking for an alternative to Java Bitset implementation. I am implementing a high performance algorithm and seems like using a Bitset object is killing its performance. Any ideas?
Someone here has compared boolean[]
to BitSet
and concluded with:
BitSet
is more memory efficient thanboolean[]
except for very small sizes. Eachboolean
in the array takes a byte. The numbers fromruntime.freeMemory()
are a bit muddled forBitSet
, but less.
boolean[]
is more CPU efficient except for very large sizes, where they are about even. E.g., for size 1 millionboolean[]
is about four times faster (e.g. 6ms vs 27ms), for ten and a hundred million they are about even.
If you Google, you can find some alternative implementations as well, like JavaEWAH, used by Apache Hive, Apache Spark and Eclipse JGit. It claims:
The goal of word-aligned compression is not to achieve the best compression, but rather to improve query processing time. Hence, we try to save CPU cycles, maybe at the expense of storage. However, the EWAH scheme we implemented is always more efficient storage-wise than an uncompressed bitmap as implemented in the BitSet class). Unlike some alternatives, javaewah does not rely on a patented scheme.