Question

I want to compare parts of byte[] efficiently - so I understand memcmp() should be used.

I know I can using PInvoke to call memcmp() - Comparing two byte arrays in .NET

But, I want to compare only parts of the byte[] - using offset, and there is no memcmp() with offset since it uses pointers.

int CompareBuffers(byte[] buffer1, int offset1, byte[] buffer2, int offset2, int count)
{
  // Somehow call memcmp(&buffer1+offset1, &buffer2+offset2, count)
}

Should I use C++/CLI to do that?

Should I use PInvoke with IntPtr? How?

Thank you.

Was it helpful?

Solution

C++/CLI will definitely be the cleanest, but this hardly justifies adding C++/CLI to your project if you aren't already using it.

How about Marshal.UnsafeAddrOfPinnedArrayElement(array, offset)?

OTHER TIPS

[DllImport("msvcrt.dll")]
private static extern unsafe int memcmp(byte* b1, byte* b2, int count);

public static unsafe int CompareBuffers(byte[] buffer1, int offset1, byte[] buffer2, int offset2, int count)
{
    fixed (byte* b1 = buffer1, b2 = buffer2)
    {
        return memcmp(b1 + offset1, b2 + offset2, count);
    }
}

You might also want to add some parameter validation.

No need to P/Invoke in C++/CLI. Use pin_ptr<> to pin the array. That gets you a byte*, just add the offset.

No matter what you do, you should check that the offset/count values are valid for the given byte arrays. After you do that, I don't see how just doing a for loop in C# is going to be any slower than P/Invoking a Win32 method. Seems like there would be a lot of overhead in the P/Invoke that it would not be worthwhile.

Also, there is always unsafe C#.

As with all performance questions, you should do your own performance testing. But it sounds to me like you are trying to optimize for performance prematurely.

There is one more way

SequenceEqual from System.Linq

 byte[] ByteArray1 = null;
 byte[] ByteArray2 = null;

 ByteArray1 = MyFunct1();
 ByteArray2 = MyFunct2();

 if (ByteArray1.SequenceEqual<byte>(ByteArray2))
 {
    MessageBox.Show("Same");
 }
 else
 {
   MessageBox.Show("Not Equal");
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top