1

I have a set of arrays of hundreds of thousands flags 0 or 1.

I'm using the BitArray class for doing something like that:

result = (BitArray)ab.Clone();
result.And(bc);

many many times...

Of course I have to set the flags in Bitarray first.

for (int i = 1; i < maxLen; i++) ab[i] = a[i] < b[i];

But when I set the flags once then I'm doing thousands of operations and, or, xor, not on them (so the speed of the bitwise operations is much more important)

And again from the beginning.

I'm asking you if is in C# faster method for doing this?

6
  • 2
    You should easily be able to figure out which method is faster using the Stopwatch class to time real results in your application. This will be much more accurate than anything we can give you. Commented Jan 16, 2018 at 20:39
  • ofc.... but give me a metod to compare it with ... Commented Jan 16, 2018 at 20:42
  • I thought you were asking if it is faster to use BitArray.And compared to looping over all the bits and using & on each bit pair. If that isn't what you are asking about, please clarify your question. Commented Jan 16, 2018 at 20:48
  • this method is about 350 times slower - so no ... i'm looking for something different - maybe memory streams, or something - give me idea to looking for Commented Jan 16, 2018 at 20:55
  • 1
    Is the expense in the clone? If so, you might consider using an immutable persistent data structure; the overhead you get in the structure being larger and more complicated is paid for in the cloning operation being zero cost. Commented Jan 16, 2018 at 21:34

1 Answer 1

4

Assuming GPU isn't an option, your best bet will be Vector<byte> on a CPU that supports SIMD operations. There are methods in System.Runtime.CompilerServices.Unsafe that allow you to load a Vector<T> from an unsafe pointer (specifically: Unsafe.Read<Vector<byte>>), allowing it to be used very efficiently against arrays pinned via fixed. Or: wait until Span<T> is mainstream, and use NonPortableCast<byte, Vector<byte>>

Sign up to request clarification or add additional context in comments.

1 Comment

@barpas yes; the package is here: nuget.org/packages/System.Numerics.Vectors - the .NET JIT has had inbuilt recognition of Vector<T> for some time now, emitting optimized SIMD instructions automatically. Remember to check whether your hardware supports it - msdn.microsoft.com/en-us/library/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.