简体   繁体   English

是否有一个memcmp等效于比较Mono中的字节数组?

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

There is a well-known efficiency for comparing two byte-arrays in .Net by importing the memcmp function from msvcrt.dll , as described here . 有一种通过导入所述比较在.net两字节阵列的公知效率memcmp从功能msvcrt.dll ,如所描述这里

Is there an equivalent library import in mono? 单声道中是否有等效的库导入? Would it need to be different when running mono on linux or on windows? 在linux或Windows上运行mono时需要有所不同吗? Or is there another fast byte array comparison technique that works well in mono? 或者是否有另一种快速字节数组比较技术在单声道中运行良好? I'm looking for something better than just iterating over the arrays in c#. 我正在寻找比在c#中迭代数组更好的东西。

Update 更新

Based on Matt Patenaude's comment, I think this might work well: 根据Matt Patenaude的评论,我认为这可能会很好:

#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 yet tried it. 但我还没有尝试过。 I've never done p/invoke on mono before. 我以前从未在单声道上进行p / invoke。 I'm using the signature recommended on pinvoke.net . 我正在使用pinvoke.net上推荐的签名。 Is this going to be compatible? 这会兼容吗?

Looking for a Mono-focused answer. 寻找以单调为重点的答案。 Thanks. 谢谢。

Based on your update, you shouldn't be using the __MonoCS__ preprocessor. 根据您的更新,您不应该使用__MonoCS__预处理器。 It means you would have to recompile the library for Mono and .NET. 这意味着你必须为Mono和.NET重新编译库。 The better way is to use dllmap functionality in Mono and only use the msvcrt.dll DllImport. 更好的方法是在Mono中使用dllmap功能,并且只使用msvcrt.dll DllImport。

Instead define a "AssemblyName.dll.config" and use the dllmap tag to map msvcrt.dll to c when run on Mono. 而是定义“AssemblyName.dll.config”并使用dllmap标记在Mono上运行时将msvcrt.dll映射到c

Example: 例:

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

More detail on dllmap here: http://www.mono-project.com/Config_DllMap 有关dllmap的更多详细信息,请访问: http//www.mono-project.com/Config_DllMap

EDIT 编辑

And if for some reason c doesn't work, libc.so should work. 如果由于某种原因c不起作用, libc.so应该工作。

You can use unsafe code blocks to access byte arrays almost as fast as native memcmp . 您可以使用unsafe代码块来访问字节数组,几乎与本机memcmp一样快。 Before you go down that road, make sure a straight for loop isn't fast enough for your purposes. 你走这条道路之前,请确保直for循环速度不够快你的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM