简体   繁体   English

在Java中将两个位集相交到新的BitSet中最快的方法是什么?

[英]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 ? 也许转换为整数并添加为数字,然后再转换回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; 如果您真的需要速度,我想实际上BitSet所做的几乎没有与整数上的按位运算一样快的&和|等。当然,在按位运算上您没有很多操作。处理您在BitSet对象中执行的整数; you give that up when you decide to go to maximum speed. 当您决定达到最大速度时,您就放弃了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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