M
Momo
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
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