首页 文章

比较.NET中的两个字节数组

提问于
浏览
457

我怎么能快速做到这一点?

当然,我可以这样做:

static bool ByteArrayCompare(byte[] a1, byte[] a2)
{
    if (a1.Length != a2.Length)
        return false;

    for (int i=0; i<a1.Length; i++)
        if (a1[i]!=a2[i])
            return false;

    return true;
}

但我正在寻找一个BCL功能或一些经过高度优化的可靠方法来实现这一目标 .

java.util.Arrays.equals((sbyte[])(Array)a1, (sbyte[])(Array)a2);

很好地工作,但它看起来不适用于x64 .

请注意我的超快答案here .

28 回答

  • 0

    如果您不反对这样做,可以导入J#assembly "vjslib.dll"并使用其Arrays.equals(byte[], byte[]) method ...

    如果有人嘲笑你,不要怪我...


    编辑:为了它的 Value ,我使用Reflector来反汇编代码,这就是它的样子:

    public static bool equals(sbyte[] a1, sbyte[] a2)
    {
      if (a1 == a2)
      {
        return true;
      }
      if ((a1 != null) && (a2 != null))
      {
        if (a1.Length != a2.Length)
        {
          return false;
        }
        for (int i = 0; i < a1.Length; i++)
        {
          if (a1[i] != a2[i])
          {
            return false;
          }
        }
        return true;
      }
      return false;
    }
    
  • -2

    您可以使用Enumerable.SequenceEqual方法 .

    using System;
    using System.Linq;
    ...
    var a1 = new int[] { 1, 2, 3};
    var a2 = new int[] { 1, 2, 3};
    var a3 = new int[] { 1, 2, 4};
    var x = a1.SequenceEqual(a2); // true
    var y = a1.SequenceEqual(a3); // false
    

    如果由于某种原因无法使用.NET 3.5,那么您的方法就可以了 .
    编译器\运行时环境将优化您的循环,因此您不必担心性能 .

  • 17

    P/Invoke权力激活!

    [DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
    static extern int memcmp(byte[] b1, byte[] b2, long count);
    
    static bool ByteArrayCompare(byte[] b1, byte[] b2)
    {
        // Validate buffers are the same length.
        // This also ensures that the count does not exceed the length of either buffer.  
        return b1.Length == b2.Length && memcmp(b1, b2, b1.Length) == 0;
    }
    
  • 545

    在.NET 4中有一个新的内置解决方案 - IStructuralEquatable

    static bool ByteArrayCompare(byte[] a1, byte[] a2) 
    {
        return StructuralComparisons.StructuralEqualityComparer.Equals(a1, a2);
    }
    
  • -2

    用户gil提出了产生此解决方案的不安全代码:

    // Copyright (c) 2008-2013 Hafthor Stefansson
    // Distributed under the MIT/X11 software license
    // Ref: http://www.opensource.org/licenses/mit-license.php.
    static unsafe bool UnsafeCompare(byte[] a1, byte[] a2) {
      if(a1==a2) return true;
      if(a1==null || a2==null || a1.Length!=a2.Length)
        return false;
      fixed (byte* p1=a1, p2=a2) {
        byte* x1=p1, x2=p2;
        int l = a1.Length;
        for (int i=0; i < l/8; i++, x1+=8, x2+=8)
          if (*((long*)x1) != *((long*)x2)) return false;
        if ((l & 4)!=0) { if (*((int*)x1)!=*((int*)x2)) return false; x1+=4; x2+=4; }
        if ((l & 2)!=0) { if (*((short*)x1)!=*((short*)x2)) return false; x1+=2; x2+=2; }
        if ((l & 1)!=0) if (*((byte*)x1) != *((byte*)x2)) return false;
        return true;
      }
    }
    

    这对于尽可能多的阵列进行基于64位的比较 . 这种情况取决于数组启动qword对齐的事实 . 如果没有qword对齐它就会起作用,只是没有像它那样快 .

    它比简单的 for 循环执行大约七个定时器 . 使用J#库与原始 for 循环等效地执行 . 使用.SequenceEqual运行大约慢七倍;我想是因为它使用的是IEnumerator.MoveNext . 我认为基于LINQ的解决方案至少要慢或者差 .

  • -1

    如果你看一下.NET如何执行string.Equals,你会看到它使用一个名为EqualsHelper的私有方法,该方法具有"unsafe"指针实现 . .NET Reflector是你的朋友,看看内部是如何完成的 .

    这可以用作字节数组比较的模板,我在博客文章Fast byte array comparison in C#中进行了实现 . 我还做了一些基本的基准测试,看看安全实施何时比不安全更快 .

    也就是说,除非你真的需要杀手性能,否则我会选择简单的fr循环比较 .

  • 0

    .NET 3.5和更新版本有一个新的公共类型, System.Data.Linq.Binary 封装 byte[] . 它实现 IEquatable<Binary> (实际上)比较两个字节数组 . 注意 System.Data.Linq.Binary 也有来自 byte[] 的隐式转换运算符 .

    MSDN文档:System.Data.Linq.Binary

    反射器反编译的Equals方法:

    private bool EqualsTo(Binary binary)
    {
        if (this != binary)
        {
            if (binary == null)
            {
                return false;
            }
            if (this.bytes.Length != binary.bytes.Length)
            {
                return false;
            }
            if (this.hashCode != binary.hashCode)
            {
                return false;
            }
            int index = 0;
            int length = this.bytes.Length;
            while (index < length)
            {
                if (this.bytes[index] != binary.bytes[index])
                {
                    return false;
                }
                index++;
            }
        }
        return true;
    }
    

    有趣的是,如果两个二进制对象的哈希值相同,它们只会进行逐字节比较循环 . 然而,这是以在 Binary 对象的构造函数中计算哈希为代价的(通过使用 for 循环遍历数组:-)) .

    上面的实现意味着在最坏的情况下你可能需要遍历数组三次:首先计算array1的散列,然后计算array2的散列,最后(因为这是最坏的情况,长度和散列相等)来比较array1中的字节,数组2中的字节数 .

    总的来说,即使 System.Data.Linq.Binary 内置于BCL中,我也不认为这是比较两个字节数组的最快方法: - | .

  • -1

    Span<T> 提供极具竞争力的替代方案,无需将混乱和/或不便携的绒毛扔进您自己的应用程序代码库:

    // byte[] is implicitly convertible to ReadOnlySpan<byte>
    static bool ByteArrayCompare(ReadOnlySpan<byte> a1, ReadOnlySpan<byte> a2)
    {
        return a1.SequenceEqual(a2);
    }
    

    (实施的)内容可以找到here .

    我已经revised @ EliArbel的要点是将此方法添加为 SpansEqual ,删除其他基准测试中大多数不那么有趣的执行者,使用不同的数组大小运行它,输出图形并标记 SpansEqual 作为基线,以便它报告不同的方法与 SpansEqual 比较 .

    以下数字来自结果,轻微编辑以删除“错误”列 .

    |        Method |  ByteCount |               Mean |         StdDev | Scaled |
    |-------------- |----------- |-------------------:|---------------:|-------:|
    |    SpansEqual |         15 |           3.614 ns |      0.0069 ns |   1.00 |
    |  LongPointers |         15 |           4.762 ns |      0.0009 ns |   1.32 |
    |      Unrolled |         15 |          16.933 ns |      0.0024 ns |   4.68 |
    | PInvokeMemcmp |         15 |          11.448 ns |      0.0183 ns |   3.17 |
    |               |            |                    |                |        |
    |    SpansEqual |       1026 |          25.957 ns |      0.0081 ns |   1.00 |
    |  LongPointers |       1026 |          60.336 ns |      0.0211 ns |   2.32 |
    |      Unrolled |       1026 |          37.216 ns |      0.0042 ns |   1.43 |
    | PInvokeMemcmp |       1026 |          43.531 ns |      0.0229 ns |   1.68 |
    |               |            |                    |                |        |
    |    SpansEqual |    1048585 |      42,708.279 ns |      6.7683 ns |   1.00 |
    |  LongPointers |    1048585 |      57,952.010 ns |      6.0004 ns |   1.36 |
    |      Unrolled |    1048585 |      52,768.967 ns |      5.1800 ns |   1.24 |
    | PInvokeMemcmp |    1048585 |      53,270.846 ns |     11.9056 ns |   1.25 |
    |               |            |                    |                |        |
    |    SpansEqual | 2147483591 | 243,281,911.498 ns | 65,006.3172 ns |   1.00 |
    |  LongPointers | 2147483591 | 237,786,969.675 ns | 96,332.7202 ns |   0.98 |
    |      Unrolled | 2147483591 | 237,151,053.500 ns | 74,137.6513 ns |   0.97 |
    | PInvokeMemcmp | 2147483591 | 235,829,644.641 ns | 50,390.2144 ns |   0.97 |
    

    我很惊讶地看到 SpansEqual 并没有出现在max-array-size方法的顶部,但差别非常小,以至于我无论如何都不重要 .

    我的系统信息:

    BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134
    Intel Core i7-6850K CPU 3.60GHz (Skylake), 1 CPU, 12 logical and 6 physical cores
    Frequency=3515619 Hz, Resolution=284.4449 ns, Timer=TSC
    .NET Core SDK=2.1.300
      [Host]     : .NET Core 2.1.0 (CoreCLR 4.6.26515.07, CoreFX 4.6.26515.06), 64bit RyuJIT
      DefaultJob : .NET Core 2.1.0 (CoreCLR 4.6.26515.07, CoreFX 4.6.26515.06), 64bit RyuJIT
    
  • 69

    I posted关于检查byte []是否充满零的类似问题 . (SIMD代码遭到殴打,所以我从这个答案中删除了它 . )这是我比较中最快的代码:

    static unsafe bool EqualBytesLongUnrolled (byte[] data1, byte[] data2)
    {
        if (data1 == data2)
            return true;
        if (data1.Length != data2.Length)
            return false;
    
        fixed (byte* bytes1 = data1, bytes2 = data2) {
            int len = data1.Length;
            int rem = len % (sizeof(long) * 16);
            long* b1 = (long*)bytes1;
            long* b2 = (long*)bytes2;
            long* e1 = (long*)(bytes1 + len - rem);
    
            while (b1 < e1) {
                if (*(b1) != *(b2) || *(b1 + 1) != *(b2 + 1) || 
                    *(b1 + 2) != *(b2 + 2) || *(b1 + 3) != *(b2 + 3) ||
                    *(b1 + 4) != *(b2 + 4) || *(b1 + 5) != *(b2 + 5) || 
                    *(b1 + 6) != *(b2 + 6) || *(b1 + 7) != *(b2 + 7) ||
                    *(b1 + 8) != *(b2 + 8) || *(b1 + 9) != *(b2 + 9) || 
                    *(b1 + 10) != *(b2 + 10) || *(b1 + 11) != *(b2 + 11) ||
                    *(b1 + 12) != *(b2 + 12) || *(b1 + 13) != *(b2 + 13) || 
                    *(b1 + 14) != *(b2 + 14) || *(b1 + 15) != *(b2 + 15))
                    return false;
                b1 += 16;
                b2 += 16;
            }
    
            for (int i = 0; i < rem; i++)
                if (data1 [len - 1 - i] != data2 [len - 1 - i])
                    return false;
    
            return true;
        }
    }
    

    在两个256MB字节数组上测量:

    UnsafeCompare                           : 86,8784 ms
    EqualBytesSimd                          : 71,5125 ms
    EqualBytesSimdUnrolled                  : 73,1917 ms
    EqualBytesLongUnrolled                  : 39,8623 ms
    
  • 25
    using System.Linq; //SequenceEqual
    
     byte[] ByteArray1 = null;
     byte[] ByteArray2 = null;
    
     ByteArray1 = MyFunct1();
     ByteArray2 = MyFunct2();
    
     if (ByteArray1.SequenceEqual<byte>(ByteArray2) == true)
     {
        MessageBox.Show("Match");
     }
     else
     {
       MessageBox.Show("Don't match");
     }
    
  • 0

    我们再添加一个!

    最近微软发布了一个特殊的NuGet包,System.Runtime.CompilerServices.Unsafe . 它's special because it'写在IL中,并提供C#中不能直接使用的低级功能 .

    其中一个方法 Unsafe.As<T>(object) 允许将任何引用类型转换为另一个引用类型,跳过任何安全检查 . 这通常是一个坏主意,但如果两种类型具有相同的结构,它可以工作 . 所以我们可以使用它来将 byte[] 转换为 long[]

    bool CompareWithUnsafeLibrary(byte[] a1, byte[] a2)
    {
        if (a1.Length != a2.Length) return false;
    
        var longSize = (int)Math.Floor(a1.Length / 8.0);
        var long1 = Unsafe.As<long[]>(a1);
        var long2 = Unsafe.As<long[]>(a2);
    
        for (var i = 0; i < longSize; i++)
        {
            if (long1[i] != long2[i]) return false;
        }
    
        for (var i = longSize * 8; i < a1.Length; i++)
        {
            if (a1[i] != a2[i]) return false;
        }
    
        return true;
    }
    

    请注意, long1.Length 仍将返回存储在数组内存结构中的字段中的原始数组's length, since it' .

    这个方法并不像这里演示的其他方法那么快,但它比朴素方法快得多,不使用不安全的代码或P / Invoke或pinning,并且实现非常简单(IMO) . 以下是我机器的一些BenchmarkDotNet结果:

    BenchmarkDotNet=v0.10.3.0, OS=Microsoft Windows NT 6.2.9200.0
    Processor=Intel(R) Core(TM) i7-4870HQ CPU 2.50GHz, ProcessorCount=8
    Frequency=2435775 Hz, Resolution=410.5470 ns, Timer=TSC
      [Host]     : Clr 4.0.30319.42000, 64bit RyuJIT-v4.6.1637.0
      DefaultJob : Clr 4.0.30319.42000, 64bit RyuJIT-v4.6.1637.0
    
                     Method |          Mean |    StdDev |
    ----------------------- |-------------- |---------- |
              UnsafeLibrary |   125.8229 ns | 0.3588 ns |
              UnsafeCompare |    89.9036 ns | 0.8243 ns |
               JSharpEquals | 1,432.1717 ns | 1.3161 ns |
     EqualBytesLongUnrolled |    43.7863 ns | 0.8923 ns |
                  NewMemCmp |    65.4108 ns | 0.2202 ns |
                ArraysEqual |   910.8372 ns | 2.6082 ns |
              PInvokeMemcmp |    52.7201 ns | 0.1105 ns |
    

    我也创建了一个gist with all the tests .

  • 16

    我会使用不安全的代码并运行比较Int32指针的 for 循环 .

    也许您还应该考虑将数组检查为非null .

  • 24

    我开发了一种方法,略微超过了 memcmp() (基座的答案)并且非常轻微地打败了 EqualBytesLongUnrolled() (Arek Bulski的回答) . 基本上,它将循环展开4而不是8 .

    public static unsafe bool NewMemCmp(byte* b0, byte* b1, int length)
    {
        byte* lastAddr = b0 + length;
        byte* lastAddrMinus32 = lastAddr - 32;
        while (b0 < lastAddrMinus32) // unroll the loop so that we are comparing 32 bytes at a time.
        {
            if (*(ulong*)b0 != *(ulong*)b1) return false;
            if (*(ulong*)(b0 + 8) != *(ulong*)(b1 + 8)) return false;
            if (*(ulong*)(b0 + 16) != *(ulong*)(b1 + 16)) return false;
            if (*(ulong*)(b0 + 24) != *(ulong*)(b1 + 24)) return false;
            b0 += 32;
            b1 += 32;
        }
        while (b0 < lastAddr)
        {
            if (*b0 != *b1) return false;
            b0++;
            b1++;
        }
        return true;
    }
    
    public static unsafe bool NewMemCmp(byte[] arr0, byte[] arr1, int length)
    {
        fixed (byte* b0 = arr0, b1 = arr1)
        {
            return b0 == b1 || NewMemCmp(b0, b1, length);
        }
    }
    

    这比_679717快约25%,比快6%快 EqualBytesLongUnrolled() 在我的机器上 .

  • 0

    似乎EqualBytesLongUnrolled是上面建议的最好的 .

    跳过的方法(Enumerable.SequenceEqual,StructuralComparisons.StructuralEqualityComparer.Equals),不是慢病人 . 在265MB阵列上我测量了这个:

    Host Process Environment Information:
    BenchmarkDotNet.Core=v0.9.9.0
    OS=Microsoft Windows NT 6.2.9200.0
    Processor=Intel(R) Core(TM) i7-3770 CPU 3.40GHz, ProcessorCount=8
    Frequency=3323582 ticks, Resolution=300.8802 ns, Timer=TSC
    CLR=MS.NET 4.0.30319.42000, Arch=64-bit RELEASE [RyuJIT]
    GC=Concurrent Workstation
    JitModules=clrjit-v4.6.1590.0
    
    Type=CompareMemoriesBenchmarks  Mode=Throughput  
    
                     Method |      Median |    StdDev | Scaled | Scaled-SD |
    ----------------------- |------------ |---------- |------- |---------- |
                 NewMemCopy |  30.0443 ms | 1.1880 ms |   1.00 |      0.00 |
     EqualBytesLongUnrolled |  29.9917 ms | 0.7480 ms |   0.99 |      0.04 |
              msvcrt_memcmp |  30.0930 ms | 0.2964 ms |   1.00 |      0.03 |
              UnsafeCompare |  31.0520 ms | 0.7072 ms |   1.03 |      0.04 |
           ByteArrayCompare | 212.9980 ms | 2.0776 ms |   7.06 |      0.25 |
    

    OS=Windows
    Processor=?, ProcessorCount=8
    Frequency=3323582 ticks, Resolution=300.8802 ns, Timer=TSC
    CLR=CORE, Arch=64-bit ? [RyuJIT]
    GC=Concurrent Workstation
    dotnet cli version: 1.0.0-preview2-003131
    
    Type=CompareMemoriesBenchmarks  Mode=Throughput  
    
                     Method |      Median |    StdDev | Scaled | Scaled-SD |
    ----------------------- |------------ |---------- |------- |---------- |
                 NewMemCopy |  30.1789 ms | 0.0437 ms |   1.00 |      0.00 |
     EqualBytesLongUnrolled |  30.1985 ms | 0.1782 ms |   1.00 |      0.01 |
              msvcrt_memcmp |  30.1084 ms | 0.0660 ms |   1.00 |      0.00 |
              UnsafeCompare |  31.1845 ms | 0.4051 ms |   1.03 |      0.01 |
           ByteArrayCompare | 212.0213 ms | 0.1694 ms |   7.03 |      0.01 |
    
  • 7

    为了比较短字节数组,以下是一个有趣的黑客:

    if(myByteArray1.Length != myByteArray2.Length) return false;
    if(myByteArray1.Length == 8)
       return BitConverter.ToInt64(myByteArray1, 0) == BitConverter.ToInt64(myByteArray2, 0); 
    else if(myByteArray.Length == 4)
       return BitConverter.ToInt32(myByteArray2, 0) == BitConverter.ToInt32(myByteArray2, 0);
    

    然后我可能会遇到问题中列出的解决方案 .

    对此代码进行性能分析会很有趣 .

  • -5

    无法找到我完全满意的解决方案(合理的性能,但没有不安全的代码/ pinvoke)所以我想出了这个,没有什么真正的原创,但有效:

    /// <summary>
        /// 
        /// </summary>
        /// <param name="array1"></param>
        /// <param name="array2"></param>
        /// <param name="bytesToCompare"> 0 means compare entire arrays</param>
        /// <returns></returns>
        public static bool ArraysEqual(byte[] array1, byte[] array2, int bytesToCompare = 0)
        {
            if (array1.Length != array2.Length) return false;
    
            var length = (bytesToCompare == 0) ? array1.Length : bytesToCompare;
            var tailIdx = length - length % sizeof(Int64);
    
            //check in 8 byte chunks
            for (var i = 0; i < tailIdx; i += sizeof(Int64))
            {
                if (BitConverter.ToInt64(array1, i) != BitConverter.ToInt64(array2, i)) return false;
            }
    
            //check the remainder of the array, always shorter than 8 bytes
            for (var i = tailIdx; i < length; i++)
            {
                if (array1[i] != array2[i]) return false;
            }
    
            return true;
        }
    

    性能与此页面上的其他一些解决方案相比:

    简单循环:19837年,1.00

    • BitConverter:4886滴答,4.06

    不安全比较:1636蜱,12.12

    EqualBytesLongUnrolled:637个刻度,31.09

    P / Invoke memcmp:369个刻度,53.67

    在linqpad中测试,1000000字节相同的数组(最坏情况),每次500次迭代 .

  • 1

    我想到了许多显卡内置的块传输加速方法 . 但是你必须按字节顺序复制所有数据,所以如果你不想在非托管和硬件相关的代码中实现逻辑的整个部分,这对你没有多大帮助......

    与上述方法类似的另一种优化方法是从一开始就将尽可能多的数据存储在long []而不是byte []中,例如,如果您从二进制文件中顺序读取它,或者如果使用内存映射文件,请将数据读入long []或单个long值 . 然后,你的比较循环只需要为包含相同数据量的byte []所需的迭代次数的1/8 . 需要比较的时间和频率与您需要以逐字节方式访问数据的时间和频率有关,例如:在API调用中将它用作需要byte []的方法中的参数 . 最后,你只能知道你是否真的知道用例......

  • 10

    我使用附加的程序.net 4.7发布版本进行了一些测量,没有附加调试器 . 我认为人们一直在使用错误的度量标准,因为如果您关心速度,那么您需要多长时间才能确定两个字节数组是否相等 . 即以字节为单位的吞吐

    StructuralComparison :       2838.8 MiB/s
    for                  :   30553811.0 MiB/s
    ToUInt32             :   23864406.8 MiB/s
    ToUInt64             :    5526595.7 MiB/s
    memcmp               : 1848977556.1 MiB/s
    

    正如你所看到的,没有比_679727更好的方法了,它的速度要快几个数量级 . 一个简单的 for 循环是第二个最佳选择 . 而且我仍然难以理解为什么微软不能简单地包含一个 Buffer.Compare 方法 .

    [Program.cs中]:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace memcmp
    {
        class Program
        {
            static byte[] TestVector(int size)
            {
                var data = new byte[size];
                using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
                {
                    rng.GetBytes(data);
                }
                return data;
            }
    
            static TimeSpan Measure(string testCase, TimeSpan offset, Action action, bool ignore = false)
            {
                var t = Stopwatch.StartNew();
                var n = 0L;
                while (t.Elapsed < TimeSpan.FromSeconds(10))
                {
                    action();
                    n++;
                }
                var elapsed = t.Elapsed - offset;
                if (!ignore)
                {
                    Console.WriteLine($"{testCase,-16} : {n / elapsed.TotalSeconds,16:0.0} MiB/s");
                }
                return elapsed;
            }
    
            [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl)]
            static extern int memcmp(byte[] b1, byte[] b2, long count);
    
            static void Main(string[] args)
            {
                // how quickly can we establish if two sequences of bytes are equal?
    
                // note that we are testing the speed of different comparsion methods
    
                var a = TestVector(1024 * 1024); // 1 MiB
                var b = (byte[])a.Clone();
    
                var offset = Measure("offset", new TimeSpan(), () => { return; }, ignore: true);
    
                Measure("StructuralComparison", offset, () =>
                {
                    StructuralComparisons.StructuralEqualityComparer.Equals(a, b);
                });
    
                Measure("for", offset, () =>
                {
                    for (int i = 0; i < a.Length; i++)
                    {
                        if (a[i] != b[i]) break;
                    }
                });
    
                Measure("ToUInt32", offset, () =>
                {
                    for (int i = 0; i < a.Length; i += 4)
                    {
                        if (BitConverter.ToUInt32(a, i) != BitConverter.ToUInt32(b, i)) break;
                    }
                });
    
                Measure("ToUInt64", offset, () =>
                {
                    for (int i = 0; i < a.Length; i += 8)
                    {
                        if (BitConverter.ToUInt64(a, i) != BitConverter.ToUInt64(b, i)) break;
                    }
                });
    
                Measure("memcmp", offset, () =>
                {
                    memcmp(a, b, a.Length);
                });
            }
        }
    }
    
  • 1

    对不起,如果您正在寻找一种托管方式,那么您已经正确地进行了操作,据我所知,BCL中没有内置方法来执行此操作 .

    您应该添加一些初始空值检查,然后只需将其重用,就像它在BCL中的位置一样 .

  • 6

    这几乎肯定比这里给出的任何其他版本慢得多,但写起来很有趣 .

    static bool ByteArrayEquals(byte[] a1, byte[] a2) 
    {
        return a1.Zip(a2, (l, r) => l == r).All(x => x);
    }
    
  • 225

    我这里没有看到很多linq解决方案 .

    我不确定性能的影响,但是我通常坚持使用 linq 作为经验法则,然后在必要时进行优化 .

    public bool CompareTwoArrays(byte[] array1, byte[] array2)
     {
       return !array1.Where((t, i) => t != array2[i]).Any();
     }
    

    请注意,这只适用于尺寸相同的阵列 . 扩展可能看起来像这样

    public bool CompareTwoArrays(byte[] array1, byte[] array2)
     {
       if (array1.Length != array2.Length) return false;
       return !array1.Where((t, i) => t != array2[i]).Any();
     }
    
  • 6

    我决定采用ArekBulski发布的EqualBytesLongUnrolled方法启发的解决方案,并进行额外的优化 . 在我的实例中,数组中的数组差异往往接近数组的尾部 . 在测试中,我发现当大型阵列就是这种情况时,能够以相反的顺序比较阵列元素,这使得该解决方案比基于memcmp的解决方案具有巨大的性能提升 . 这是解决方案:

    public enum CompareDirection { Forward, Backward }
    
    private static unsafe bool UnsafeEquals(byte[] a, byte[] b, CompareDirection direction = CompareDirection.Forward)
    {
        // returns when a and b are same array or both null
        if (a == b) return true;
    
        // if either is null or different lengths, can't be equal
        if (a == null || b == null || a.Length != b.Length)
            return false;
    
        const int UNROLLED = 16;                // count of longs 'unrolled' in optimization
        int size = sizeof(long) * UNROLLED;     // 128 bytes (min size for 'unrolled' optimization)
        int len = a.Length;
        int n = len / size;         // count of full 128 byte segments
        int r = len % size;         // count of remaining 'unoptimized' bytes
    
        // pin the arrays and access them via pointers
        fixed (byte* pb_a = a, pb_b = b)
        {
            if (r > 0 && direction == CompareDirection.Backward)
            {
                byte* pa = pb_a + len - 1;
                byte* pb = pb_b + len - 1;
                byte* phead = pb_a + len - r;
                while(pa >= phead)
                {
                    if (*pa != *pb) return false;
                    pa--;
                    pb--;
                }
            }
    
            if (n > 0)
            {
                int nOffset = n * size;
                if (direction == CompareDirection.Forward)
                {
                    long* pa = (long*)pb_a;
                    long* pb = (long*)pb_b;
                    long* ptail = (long*)(pb_a + nOffset);
                    while (pa < ptail)
                    {
                        if (*(pa + 0) != *(pb + 0) || *(pa + 1) != *(pb + 1) ||
                            *(pa + 2) != *(pb + 2) || *(pa + 3) != *(pb + 3) ||
                            *(pa + 4) != *(pb + 4) || *(pa + 5) != *(pb + 5) ||
                            *(pa + 6) != *(pb + 6) || *(pa + 7) != *(pb + 7) ||
                            *(pa + 8) != *(pb + 8) || *(pa + 9) != *(pb + 9) ||
                            *(pa + 10) != *(pb + 10) || *(pa + 11) != *(pb + 11) ||
                            *(pa + 12) != *(pb + 12) || *(pa + 13) != *(pb + 13) ||
                            *(pa + 14) != *(pb + 14) || *(pa + 15) != *(pb + 15)
                        )
                        {
                            return false;
                        }
                        pa += UNROLLED;
                        pb += UNROLLED;
                    }
                }
                else
                {
                    long* pa = (long*)(pb_a + nOffset);
                    long* pb = (long*)(pb_b + nOffset);
                    long* phead = (long*)pb_a;
                    while (phead < pa)
                    {
                        if (*(pa - 1) != *(pb - 1) || *(pa - 2) != *(pb - 2) ||
                            *(pa - 3) != *(pb - 3) || *(pa - 4) != *(pb - 4) ||
                            *(pa - 5) != *(pb - 5) || *(pa - 6) != *(pb - 6) ||
                            *(pa - 7) != *(pb - 7) || *(pa - 8) != *(pb - 8) ||
                            *(pa - 9) != *(pb - 9) || *(pa - 10) != *(pb - 10) ||
                            *(pa - 11) != *(pb - 11) || *(pa - 12) != *(pb - 12) ||
                            *(pa - 13) != *(pb - 13) || *(pa - 14) != *(pb - 14) ||
                            *(pa - 15) != *(pb - 15) || *(pa - 16) != *(pb - 16)
                        )
                        {
                            return false;
                        }
                        pa -= UNROLLED;
                        pb -= UNROLLED;
                    }
                }
            }
    
            if (r > 0 && direction == CompareDirection.Forward)
            {
                byte* pa = pb_a + len - r;
                byte* pb = pb_b + len - r;
                byte* ptail = pb_a + len;
                while(pa < ptail)
                {
                    if (*pa != *pb) return false;
                    pa++;
                    pb++;
                }
            }
        }
    
        return true;
    }
    
  • 5

    使用 SequenceEquals 进行比较 .

  • -1

    简短的回答是这样的:

    public bool Compare(byte[] b1, byte[] b2)
        {
            return Encoding.ASCII.GetString(b1) == Encoding.ASCII.GetString(b2);
        }
    

    通过这种方式,您可以使用优化的.NET字符串比较来进行字节数组比较,而无需编写不安全的代码 . 这是在background中完成的:

    private unsafe static bool EqualsHelper(String strA, String strB)
    {
        Contract.Requires(strA != null);
        Contract.Requires(strB != null);
        Contract.Requires(strA.Length == strB.Length);
    
        int length = strA.Length;
    
        fixed (char* ap = &strA.m_firstChar) fixed (char* bp = &strB.m_firstChar)
        {
            char* a = ap;
            char* b = bp;
    
            // Unroll the loop
    
            #if AMD64
                // For the AMD64 bit platform we unroll by 12 and
                // check three qwords at a time. This is less code
                // than the 32 bit case and is shorter
                // pathlength.
    
                while (length >= 12)
                {
                    if (*(long*)a     != *(long*)b)     return false;
                    if (*(long*)(a+4) != *(long*)(b+4)) return false;
                    if (*(long*)(a+8) != *(long*)(b+8)) return false;
                    a += 12; b += 12; length -= 12;
                }
           #else
               while (length >= 10)
               {
                   if (*(int*)a != *(int*)b) return false;
                   if (*(int*)(a+2) != *(int*)(b+2)) return false;
                   if (*(int*)(a+4) != *(int*)(b+4)) return false;
                   if (*(int*)(a+6) != *(int*)(b+6)) return false;
                   if (*(int*)(a+8) != *(int*)(b+8)) return false;
                   a += 10; b += 10; length -= 10;
               }
           #endif
    
            // This depends on the fact that the String objects are
            // always zero terminated and that the terminating zero is not included
            // in the length. For odd string sizes, the last compare will include
            // the zero terminator.
            while (length > 0)
            {
                if (*(int*)a != *(int*)b) break;
                a += 2; b += 2; length -= 2;
            }
    
            return (length <= 0);
        }
    }
    
  • 150

    由于上面的许多奇特的解决方案都不适用于UWP,因为我喜欢Linq和功能方法,所以我向你提出了我的版本问题 . 为了在第一个差异发生时逃避比较,我选择.FirstOrDefault()

    public static bool CompareByteArrays(byte[] ba0, byte[] ba1) =>
        !(ba0.Length != ba1.Length || Enumerable.Range(1,ba0.Length)
            .FirstOrDefault(n => ba0[n] != ba1[n]) > 0);
    
  • 3

    如果您正在寻找一个非常快速的字节数组相等比较器,我建议您看一下这个STSdb实验室文章:Byte array equality comparer.它具有一些最快的byte []数组相等比较实现,它们被呈现,性能测试和总结 .

    您还可以专注于这些实现:

    BigEndianByteArrayComparer - 快速byte []数组比较器从左到右(BigEndian)BigEndianByteArrayEqualityComparer - 快速字节[]相等比较器从左到右(BigEndian)LittleEndianByteArrayComparer - 快速byte []数组比较器从右到左(LittleEndian)LittleEndianByteArrayEqualityComparer - 快速字节[]从右到左的平等比较(LittleEndian)

  • 2

    有点蛮力,但它直接将字节数组转换为Base64字符串并比较两个字符串 . 如果您有大型数组可供比较,那就太方便了 . 或者,如果其中一个字节数组已经是Base64格式 .

    static bool ByteArrayCompare(byte[] a1, byte[] a2)
    {
        string s1 = Convert.ToBase64String(a1);
        string s2 = Convert.ToBase64String(a2);
        if(s1 == s2) return true;
        return false
    }
    

    我想最快的方法(对于大型数组具有最佳性能)是散列两个字节数组并比较散列 .

  • 2

    如果你有一个巨大的字节数组,你可以通过将它们转换为字符串来比较它们 .

    你可以使用类似的东西

    byte[] b1 = // Your array
    byte[] b2 = // Your array
    string s1 = Encoding.Default.GetString( b1 );
    string s2 = Encoding.Default.GetString( b2 );
    

    我使用过这个,我看到了巨大的性能影响 .

相关问题