开发者

efficiently compare two BitArrays of the same length

开发者 https://www.devze.com 2023-02-04 16:47 出处:网络
How would I do this? I am trying to count when both arrays have the same value of TRUE/1 at the same index. As you can see, my code has multiple bitarrays and is looping through each one and comparing

How would I do this? I am trying to count when both arrays have the same value of TRUE/1 at the same index. As you can see, my code has multiple bitarrays and is looping through each one and comparing them with a comparisonArray with another loo开发者_JS百科p. It doesn't seem to be very efficient and I need it to be.

foreach (bitArrayTuple in bitarryList) { 
    for (int i = 0; i < arrayLength; i++)
        if (bArrayTuple.Item2[i] && comparisonArray[i])
            bitArrayTuple.Item1++;
}

where Item1 is the count and Item2 is a bitarray.


bool equals = ba1.Xor(ba2).OfType<bool>().All(e => !e);


There's not much of a way to do this, because BitArray doesn't let its internal array leak, and because .NET doesn't have the C++ equivalent of const to prevent external modification. You might want to just create your own class from scratch, or, if you feel like hacking, use reflection to get the private field inside the BitArray.


Would this work?

http://msdn.microsoft.com/en-us/library/system.collections.bitarray.and%28v=VS.90%29.aspx

It's like the single & operator in C.


Depending in the number of elements, BitVector32 may be usable. That would simply be an Int32 comparison.

If not possible, you will need to get hold of the int[] located on the m_array private field of each BitArray. Then compare the int[] of each (which is a comparison of 32 bits at a time).


I realize this is an old thread, but I've recently run into a need for this myself and have performed some benchmarks in order to determine which method is fastest:

Firstly, at the moment we can't use BitArray.Clone() because of a known bug in Microsoft's code that will not allow cloning of arrays that are larger than int.MaxValue / 32. We will need to avoid this method until they have fixed the bug.

With that in mind I have run benchmarks against 5 different implementations, all using the largest BitArray I could construct (size of int.MaxValue) with alternating bits. I have run the tests with equal and not equal arrays and resulting speed rankings are the same. Here are the implementations:

  • Implementation 1: Convert each BitArray into a byte[] and compare the arrays using the CompareTo() method.
  • Implementation 2: Convert each BitArray into a byte[] and compare the each set of bytes using an XOR operator (^).
  • Implementation 3: Convert each BitArray into a int[] and compare the arrays using the CompareTo() method.
  • Implementation 4: Convert each BitArray into a int[] and compare the each set of ints using an XOR operator (^).
  • Implementation 5: Use a for loop to iterate over each set of bool values and compare

The winner surprised me: Implementation 3.

I would have expected Implementation 4 to be the fastest, but as it turns out 3 is significantly faster.

In terms of speed, here are the implementations ranked fastest first:

  • Implementation 3
  • Implementation 4
  • Implementation 2
  • Implementation 1
  • Implementation 5

Here's my code for implementation 3:

public static bool Equals(this BitArray first, BitArray second)
{
    // Short-circuit if the arrays are not equal in size
    if (first.length != second.length)
        return false;
    
    // Convert the arrays to int[]s
    int[] firstInts = new int[(int)Math.Ceiling((decimal)first.Count / 32)];
    first.CopyTo(firstInts, 0);
    int[] secondInts = new int[(int)Math.Ceiling((decimal)second.Count / 32)];
    second.CopyTo(secondInts , 0);

    // Look for differences
    bool areDifferent = false;
    for (int i = 0; i < firstInts.Length && !areDifferent; i++)
        areDifferent = firstInts[i] != secondInts[i];

    return !areDifferent;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消