How to call memcmp() on two parts of byte[] (with offset)?

10,073

Solution 1

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)?

Solution 2

[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.

Solution 3

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.

Solution 4

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

Share:
10,073
brickner
Author by

brickner

Updated on June 04, 2022

Comments

  • brickner
    brickner almost 2 years

    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.