Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 160073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:58:14+00:00 2026-05-11T10:58:14+00:00

I am trying to compare two long bytearrays in VB.NET and have run into

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:58:14+00:00Added an answer on May 11, 2026 at 10:58 am

    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 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to compare two decimal values in Java script. I have two
I am trying to compare two text files and output the first string in
I am trying to compare two dates. I have this code which I thought
I am trying to compare two files. I will list the two file content:
I have been trying to compare two arrays. Using array_intersect presents no problems. When
I'm trying to compare two Xml files using C# code. I want to ignore
I am trying to compare two .NET arrays. Here is an obvious implementation for
I'm trying to find a good way to have version comparison between two files
I am trying to compare two strings in a simple shell script. I was
I am trying to compare two strings in Smalltalk, but I seem to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.