Performance of java.util.BitSet

T

Timo Nentwig

Hi!

How fast is (hotspotted) java.util.BitSet? I think about using it for
parsing DNS messages (and other protocols). Actually I don't like BitSet
and prefer bit ops like real programmers do ;-) but it does make the code
probably more readable and helps people to understand how DNS messages are
structured...

However, performance is of course always an issue :)

Does anybody use BitSet at all? :)

Cheers
Timo
 
M

Matt Humphrey

Timo Nentwig said:
Hi!

How fast is (hotspotted) java.util.BitSet? I think about using it for
parsing DNS messages (and other protocols). Actually I don't like BitSet
and prefer bit ops like real programmers do ;-) but it does make the code
probably more readable and helps people to understand how DNS messages are
structured...

However, performance is of course always an issue :)

Does anybody use BitSet at all? :)

I find ordinary bit operations &, |, <<, >>, etc to be perfectly readable,
especially for fixed-length bit fields that fit nicely into ints and longs.
I have only used BitSet when there will be an unknown (> 32) number of bits.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
T

Timo Nentwig

I have only used BitSet when there will be an unknown (> 32) number of
bits.

That's actually the case for DNS :) Instead of treating the header as a
collection of integers I'd treat it as one large bit set...
 
A

Adam Jenkins

Timo said:
Hi!

How fast is (hotspotted) java.util.BitSet? I think about using it for
parsing DNS messages (and other protocols). Actually I don't like BitSet
and prefer bit ops like real programmers do ;-) but it does make the
code probably more readable and helps people to understand how DNS
messages are structured...

The JLS doesn't say anything about the implementation of BitSet, so the
performance will depend on the library and JVM implementations. Sun
gives out the source code for their standard library, so you can see for
yourself how they've implemented java.util.BitSet. They use an array of
longs, with the first 64 bits in the BitSet in the first long, etc.. So
if your BitSet is less than 64 bits long, all the operations will be
done on a single long internally anyway, using the same bit operators
you'd use to implement a bitset by hand. It looks like it's implemented
pretty efficiently, but I'm sure you could do better performance-wise by
hand-coding, since the code in BitSet is all generalized for arbitrary
length bitfields.
 
N

nos

why not just write a little test program that tells
you this stuff for your machine in your environment
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top