Performance of java.util.BitSet

Discussion in 'Java' started by Timo Nentwig, Dec 9, 2003.

  1. Timo Nentwig

    Timo Nentwig Guest

    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
    Timo Nentwig, Dec 9, 2003
    #1
    1. Advertising

  2. "Timo Nentwig" <> wrote in message
    news:eek:...
    > 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 http://www.iviz.com/
    Matt Humphrey, Dec 9, 2003
    #2
    1. Advertising

  3. Timo Nentwig

    Timo Nentwig Guest

    On Tue, 09 Dec 2003 19:16:48 GMT, Matt Humphrey <> wrote:

    > 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...
    Timo Nentwig, Dec 10, 2003
    #3
  4. Timo Nentwig

    Adam Jenkins Guest

    Timo Nentwig wrote:
    > 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.
    Adam Jenkins, Dec 10, 2003
    #4
  5. Timo Nentwig

    nos Guest

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

    "Adam Jenkins" <> wrote in message
    news:...
    > Timo Nentwig wrote:
    > > 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.
    >
    nos, Dec 11, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael T. Peterson
    Replies:
    2
    Views:
    726
    Michael T. Peterson
    Sep 9, 2003
  2. mgalaty
    Replies:
    0
    Views:
    2,708
    mgalaty
    Sep 10, 2004
  3. amy j
    Replies:
    4
    Views:
    870
    Alex Hunsley
    Oct 13, 2004
  4. Joshua
    Replies:
    4
    Views:
    377
    Esmond Pitt
    May 6, 2005
  5. Ninds
    Replies:
    14
    Views:
    701
    W Karas
    Dec 3, 2012
Loading...

Share This Page