What is the fastest way to compare two byte arrays?

后端 未结 6 1592
你的背包
你的背包 2021-01-18 07:59

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 wron

6条回答
  •  不要未来只要你来
    2021-01-18 08:27

    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
    

提交回复
热议问题