Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

Speed of BitSet operations

Thread view: 
Momo - 06 Jun 2007 20:58 GMT
    Why are the BitSet manipulations so slow on large BitSets when I try
to modify the same bit twice in a row? I've been running tests on
x=new BitSet(size=1000000). If I first turn all the bits on, and then
turn all the bits off, as follows,

     for (loop=0; loop<size; loop++)
    x.set(loop);
     for (loop=0; loop<size; loop++)
    x.clear(loop);

it goes very quickly (around 150 ms). (I know that x.clear() is much
faster for clearing the BitSet; this is just to compare with below.)

    However, if I turn each bit on, and then immediately off, it goes
much slower. That is,

     for (loop=0; loop<size; loop++) {
    x.set(loop);
    x.clear(loop);
     }

takes about 25000 ms, or about 150 times longer.

    If I stagger things a little, so that I wait a little before
reflipping a bit, it gets fast again. That is,

     for (loop=1; loop<size; loop++) {
    x.set(loop);
    x.clear(loop-1);
     }

is again around 150 ms. The slowdown for immediately reflipping a bit
becomes more pronounced for larger BitSets.

    Second (or instead), is it possible to see how to Sun implements
BitSet, which would (presumably) let me figure out what's going on? A
number of old posts imply that the implementation is publicly
available, but I couldn't find it at Sun's website.

Momo
Patricia Shanahan - 06 Jun 2007 21:27 GMT
...
>     Second (or instead), is it possible to see how to Sun implements
> BitSet, which would (presumably) let me figure out what's going on? A
> number of old posts imply that the implementation is publicly
> available, but I couldn't find it at Sun's website.
...

Look for "src.zip" in your jdk install directory.

Patricia
Momo - 06 Jun 2007 22:23 GMT
> ...>    Second (or instead), is it possible to see how to Sun implements
> > BitSet, which would (presumably) let me figure out what's going on? A
[quoted text clipped - 6 lines]
>
> Patricia

Oh, it's already on my computer. OK, now I feel like a jackass.

As it turns out, Tom Hawtin has already saved me the trouble of
inspecting the BitSet implementation, but thanks, this is quite useful
for next time.

Momo
Tom Hawtin - 06 Jun 2007 21:53 GMT
>     Why are the BitSet manipulations so slow on large BitSets when I try
> to modify the same bit twice in a row? I've been running tests on

BitSet keeps track of the number of 'word in use' (for some reason). If
you clear the top set bit and it happens to be the only set bit, then it
checks through all lower bits. So repeatedly setting and clearing the
top (and only) bit is O(n^2).

Tom Hawtin
Momo - 06 Jun 2007 22:26 GMT
> >    Why are the BitSet manipulations so slow on large BitSets when I try
> > to modify the same bit twice in a row? I've been running tests on
[quoted text clipped - 5 lines]
>
> Tom Hawtin

Thank you very much! This was causing a mysterious slowdown by a
factor of 10-100 in my simulations, for certain inputs. So it's great
to have this fixed.

Momo
Roedy Green - 15 Jun 2007 11:16 GMT
>However, if I turn each bit on, and then immediately off, it goes
>much slower. That is,

You can view the source in source.zip.  It has arrays of longs. To set
a bit, it fetches the long, ors a bit mask and stores it back. If you
do it bit by bit that happens 64 times to clear one item.

to clear all bits it just has to store a 0.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.