简体   繁体   中英

What would be the fastest way to intersect two bitsets into a new BitSet in Java?

Is there something faster in time than this:

// say we alread have BitSet bs1 and bs2
BitSet bs3 = (BitSet) bs1.clone();
bs3.and(bs2);

Maybe convert to ints and add as numbers and then convert back to BitSet ?

The only alternative I can think of is

Bitset newBitset = new Bitset(bs1.size());
newBitset.or(bs1).and(bs2);

You'll need to time the two to see which is faster; my alternative may be slightly faster since you're not calling an overriding method and you're not doing any casting.

If you really need the speed, I would guess that practically nothing BitSet does will be nearly as fast as bitwise operations on integers -- &, |, etc. Of course, you don't have a number of operations on bit-wise-handled integers that you do in BitSet objects; you give that up when you decide to go to maximum speed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM