Is there a memcmp equivalent for comparing byte arrays in Mono?

There is a known efficiency in comparing two byte arrays in .Net by importing the memcmp function from msvcrt.dll , as described here .

Is there an equivalent library import in mono? Do I need to be different when running mono on linux or on windows? Or is there another quick byte comparison technique that works well in mono? I'm looking for something better than just looping through arrays in C #.

Update

Based on a comment by Matt Patenaude, I think this might work:

 #if __MonoCS__ [DllImport("c", CallingConvention = CallingConvention.Cdecl)] #else [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)] #endif public static extern int memcmp(byte[] b1, byte[] b2, UIntPtr count); 

But I have not tried it yet. I have never done p / invoke on mono before. I use the signature recommended by pinvoke.net . Will it be compatible?

Looking for a monophonic answer. Thanks.

+7
source share
2 answers

Based on your update, you should not use the __MonoCS__ preprocessor. This means that you have to recompile the library for Mono and .NET. It is better to use the dllmap functionality in Mono and use only msvcrt.dll DllImport.

Instead, define "AssemblyName.dll.config" and use the dllmap tag to map msvcrt.dll - c when working in mono.

Example:

 <configuration> <dllmap dll="msvcrt.dll" target="libc.so.6" /> </configuration> 

Read more about dllmap here: http://www.mono-project.com/Config_DllMap

EDIT

And if for some reason c does not work, libc.so should work.

+6
source

You can use unsafe code blocks to access byte arrays almost as fast as native memcmp . Before you go down this road, make sure that the straight for loop is not fast enough for your purposes.

+2
source

All Articles