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?
Using Clang 10.0 and GCC 10.1, in both cases the array of bools is faster than bitset.
BitSet is more memory efficient than boolean[] except for very small sizes. Each boolean in the array takes a byte.
As for other differences, obviously the API is different. BitSet provides a number of bitwise operations that you may need to use. A boolean array is an array. The one that works best for you will depend on your specific application requirements.
A Python interface to the fast bitsets in Sage. Bitsets are fast binary sets that store elements by toggling bits in an array of numbers. A bitset can store values between 0 and capacity - 1 , inclusive (where capacity is finite, but arbitrary).
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.
While searching an answer for my question single byte comparison vs multiple boolean comparison, I found OpenBitSet
They claim to be faster than Java BitSet and direct access to the array of words storing the bits.
I am definitely gonna try that. See if it solve your purpose too.
Look at Javolution FastBitSet : A high-performance bitset integrated with the collection framework as a set of indices and obeying the collection semantic for methods such as FastSet.size() (cardinality) or FastCollection.equals(java.lang.Object) (same set of indices).
See also http://code.google.com/p/guava-libraries/issues/detail?id=724#c3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With