6

I am trying to compare two long bytearrays in VB.NET and have run into a snag. Comparing two 50 megabyte files takes almost two minutes, so I'm clearly doing something wrong. I'm on an x64 machine with tons of memory so there are no issues there. Here is the code that I'm using at the moment and would like to change.

_Bytes and item.Bytes are the two different arrays to compare and are already the same length.

For Each B In item.Bytes
   If B <> _Bytes(I) Then
        Mismatch = True
        Exit For
   End If
   I += 1
Next

I need to be able to compare as fast as possible files that are potentially hundreds of megabytes and even possibly a gigabyte or two. Any suggests or algorithms that would be able to do this faster?

Item.bytes is an object taken from the database/filesystem that is returned to compare, because its byte length matches the item that the user wants to add. By comparing the two arrays I can then determine if the user has added something new to the DB and if not then I can just map them to the other file and not waste hard disk drive space.

[Update]

I converted the arrays to local variables of Byte() and then did the same comparison, same code and it ran in like one second (I have to benchmark it still and compare it to others), but if you do the same thing with local variables and use a generic array it becomes massively slower. I’m not sure why, but it raises a lot more questions for me about the use of arrays.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Middletone
  • 4,190
  • 12
  • 53
  • 74
  • Comparing two 50MB arrays take less than a second for me using the naive approach. You should have another issue. – Mehrdad Afshari Mar 09 '09 at 20:02
  • 1
    Check http://stackoverflow.com/q/43289/276648 which is the same question for C#. Lots of answers. I like the unsafe version http://stackoverflow.com/a/8808245/276648 as it will also run on Mono Linux. – user276648 Jan 10 '13 at 03:58

6 Answers6

17

What is the _Bytes(I) call doing? It's not loading the file each time, is it? Even with buffering, that would be bad news!

There will be plenty of ways to micro-optimise this in terms of looking at longs at a time, potentially using unsafe code etc - but I'd just concentrate on getting reasonable performance first. Clearly there's something very odd going on.

I suggest you extract the comparison code into a separate function which takes two byte arrays. That way you know you won't be doing anything odd. I'd also use a simple For loop rather than For Each in this case - it'll be simpler. Oh, and check whether the lengths are correct first :)

EDIT: Here's the code (untested, but simple enough) that I'd use. It's in C# for the minute - I'll convert it in a sec:

public static bool Equals(byte[] first, byte[] second)
{
    if (first == second)
    {
        return true;
    }
    if (first == null || second == null)
    {
        return false;
    }
    if (first.Length != second.Length)
    {
        return false;
    }
    for (int i=0; i < first.Length; i++)
    {
        if (first[i] != second[i])                
        {
            return false;
        }
    }
    return true;
}

EDIT: And here's the VB:

Public Shared Function ArraysEqual(ByVal first As Byte(), _
                                   ByVal second As Byte()) As Boolean
    If (first Is second) Then
        Return True
    End If

    If (first Is Nothing OrElse second Is Nothing) Then
        Return False
    End If
    If  (first.Length <> second.Length) Then
         Return False
    End If

    For i as Integer = 0 To first.Length - 1
        If (first(i) <> second(i)) Then
            Return False
        End If
    Next i
    Return True
End Function
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • the _Bytes(I) is an array of bytes that is already in memory. – Middletone Mar 09 '09 at 19:57
  • i is just an index to find the byte – Middletone Mar 09 '09 at 19:58
  • Amazing Jon, I'm thrilled to have the Stack Celeb helping out on this! – Middletone Mar 09 '09 at 20:04
  • Try the code I've just provided - but I'm really surprised it was taking two minutes. The above code takes about 140ms on my laptop (built with optimisation, and not running under a debugger, admittedly). – Jon Skeet Mar 09 '09 at 20:07
  • Hi Jon, your first condition in the VB code has a `Not` too many. The parens aren't needed either but they don't do any harm (`Is` == `object.ReferenceEquals` == roughly `==` for references when no `operator ==` is defined). – Konrad Rudolph Mar 09 '09 at 20:26
  • Thanks Konrad - Reflector had refactored the code somewhat, and I missed that "Not" when trying to undo its work :) – Jon Skeet Mar 09 '09 at 21:01
  • @JonSkeet can we do something similar to arrays with different dimentions to get only the unmatched items? without using Enumerable.Except – huMpty duMpty Mar 09 '12 at 12:09
  • @huMptyduMpty: It's not clear what you mean, or why you don't want to use `Enumerable.Except`? Sounds like a new question is called for. – Jon Skeet Mar 09 '12 at 12:35
  • @JonSkeet its a older version of .net framework and it doesn't let me use **.Except** . Anyway I have posted my question here **http://stackoverflow.com/questions/9633112/compare-two-arrays-with-different-dimention/9633171#comment12228018_9633171** – huMpty duMpty Mar 09 '12 at 12:40
4

The fastest way to compare two byte arrays of equal size is to use interop. Run the following code on a console application:

using System;
using System.Runtime.InteropServices;
using System.Security;

namespace CompareByteArray
{
    class Program
    {
        static void Main(string[] args)
        {
            const int SIZE = 100000;
            const int TEST_COUNT = 100;

            byte[] arrayA = new byte[SIZE];
            byte[] arrayB = new byte[SIZE];

            for (int i = 0; i < SIZE; i++)
            {
                arrayA[i] = 0x22;
                arrayB[i] = 0x22;
            }

            {
                DateTime before = DateTime.Now;
                for (int i = 0; i < TEST_COUNT; i++)
                {
                    int result = MemCmp_Safe(arrayA, arrayB, (UIntPtr)SIZE);

                    if (result != 0) throw new Exception();
                }
                DateTime after = DateTime.Now;

                Console.WriteLine("MemCmp_Safe: {0}", after - before);
            }

            {
                DateTime before = DateTime.Now;
                for (int i = 0; i < TEST_COUNT; i++)
                {
                    int result = MemCmp_Unsafe(arrayA, arrayB, (UIntPtr)SIZE);

                    if (result != 0) throw new Exception();
                }
                DateTime after = DateTime.Now;

                Console.WriteLine("MemCmp_Unsafe: {0}", after - before);
            }


            {
                DateTime before = DateTime.Now;
                for (int i = 0; i < TEST_COUNT; i++)
                {
                    int result = MemCmp_Pure(arrayA, arrayB, SIZE);

                    if (result != 0) throw new Exception();
                }
                DateTime after = DateTime.Now;

                Console.WriteLine("MemCmp_Pure: {0}", after - before);
            }
            return;
        }

        [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint="memcmp", ExactSpelling=true)]
        [SuppressUnmanagedCodeSecurity]
        static extern int memcmp_1(byte[] b1, byte[] b2, UIntPtr count);

        [DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "memcmp", ExactSpelling = true)]
        [SuppressUnmanagedCodeSecurity]
        static extern unsafe int memcmp_2(byte* b1, byte* b2, UIntPtr count);

        public static int MemCmp_Safe(byte[] a, byte[] b, UIntPtr count)
        {
            return memcmp_1(a, b, count);
        }

        public unsafe static int MemCmp_Unsafe(byte[] a, byte[] b, UIntPtr count)
        {
            fixed(byte* p_a = a)
            {
                fixed (byte* p_b = b)
                {
                    return memcmp_2(p_a, p_b, count);
                }
            }
        }

        public static int MemCmp_Pure(byte[] a, byte[] b, int count)
        {
            int result = 0;
            for (int i = 0; i < count && result == 0; i += 1)
            {
                result = a[0] - b[0];
            }

            return result;
        }

    }
}
danobrega
  • 41
  • 1
3

If you don't need to know the byte, use 64-bit ints that gives you 8 at once. Actually, you can figure out the wrong byte, once you've isolated it to a set of 8.

Use BinaryReader:

saveTime  = binReader.ReadInt32()

Or for arrays of ints:

Dim count As Integer = binReader.Read(testArray, 0, 3)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sfossen
  • 4,774
  • 24
  • 18
0

Better approach... If you are just trying to see if the two are different then save some time by not having to go through the entire byte array and generate a hash of each byte array as strings and compare the strings. MD5 should work fine and is pretty efficient.

  • It is very ridiculous thing. Any cryptographic function should scan each of array and calculate hash value for both... So it costs much more than simply perform per byte comparison. – Maxim Jun 24 '17 at 17:39
0

I see two things that might help:

First, rather than always accessing the second array as item.Bytes, use a local variable to point directly at the array. That is, before starting the loop, do something like this:

 array2 = item.Bytes

That will save the overhead of dereferencing from the object each time you want a byte. That could be expensive in Visual Basic, especially if there's a Getter method on that property.

Also, use a "definite loop" instead of "for each". You already know the length of the arrays, so just code the loop using that value. This will avoid the overhead of treating the array as a collection. The loop would look something like this:

For i = 1 to max Step 1
   If (array1(i) <> array2(i)) 
       Exit For
   EndIf 
Next
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Clayton
  • 920
  • 1
  • 6
  • 13
0

Not strictly related to the comparison algorithm:

Are you sure your bottleneck is not related to the memory available and the time used to load the byte arrays? Loading two 2 GB byte arrays just to compare them could bring most machines to their knees. If the program design allows, try using streams to read smaller chunks instead.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sergio Acosta
  • 11,418
  • 12
  • 62
  • 91