473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to compare two byte arrays ?

Is there other way of comparing two byte arrays
than iterating through the two and compare individual bytes ?

Oleg Subachev
Apr 18 '06 #1
5 39994
Is there other way of comparing two byte arrays
than iterating through the two and compare individual bytes ?


No, but you can always start by checking that their lengths are equal
first.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 18 '06 #2
Hi,

Nop, the same goes with any kind of array or collections
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Oleg Subachev" <ol**@urvb.ru> wrote in message
news:ux**************@TK2MSFTNGP02.phx.gbl...
Is there other way of comparing two byte arrays
than iterating through the two and compare individual bytes ?

Oleg Subachev

Apr 18 '06 #3
On Tue, 18 Apr 2006 10:52:08 +0600, "Oleg Subachev" <ol**@urvb.ru>
wrote:
Is there other way of comparing two byte arrays
than iterating through the two and compare individual bytes ?

Oleg Subachev


In most cases it is enough to just iterate and compare each byte. In
the rare case that maximum performance is needed you could use unsafe
code to speed up the process by using int pointers.

Here is a small code snippet that demonstrates the basics. It will
only work if the array length is divisible by 4.

public unsafe static bool Compare(byte[] b1, byte[] b2)
{
fixed (byte* bp1 = b1)
{
fixed (byte* bp2 = b2)
{
int* ip1 = (int*)bp1;
int* ip2 = (int*)bp2;

for (int i = 0; i < b1.Length / 4; i++)
{
if (ip2[i] != ip1[i])
return false;
}
return true;
}
}
}

--
Marcus Andrén
Apr 18 '06 #4


Marcus Andrén wrote:
Here is a small code snippet that demonstrates the basics. It will
only work if the array length is divisible by 4.

public unsafe static bool Compare(byte[] b1, byte[] b2)
{
fixed (byte* bp1 = b1)
{
fixed (byte* bp2 = b2)
{
int* ip1 = (int*)bp1;
int* ip2 = (int*)bp2;

for (int i = 0; i < b1.Length / 4; i++)
remember checking the length of b2 also, especially since this is unsafe
code. I would like the function to start with something like:

if ( b1.Length % 4 != 0 )
throw new ArgumentException("Length % 4 != 0", "b1");
if ( b2.Length % 4 != 0 )
throw new ArgumentException("Length % 4 != 0", "b2");
if ( b1.Length != b2.Length )
return false;
{
if (ip2[i] != ip1[i])
return false;
}
return true;
}
}
}


How much, and how have you measured that the above is faster, than simply

public static bool Compare(byte[] b1, byte[] b2) {
if ( b1.Length != b2.Length )
return false;
for ( int i = 0; i < b1.Length; ++i )
if ( b1[i] != b2[i] )
return false;
return true;
}

--
Helge Jensen
mailto:he**********@slog.dk
sip:he**********@slog.dk
-=> Sebastian cover-music: http://ungdomshus.nu <=-
Apr 19 '06 #5
On Wed, 19 Apr 2006 07:42:43 +0200, Helge Jensen
<he**********@slog.dk> wrote:

remember checking the length of b2 also, especially since this is unsafe
code. I would like the function to start with something like:
Very true.
How much, and how have you measured that the above is faster, than simply


I had done some basic measurements earlier, but since you asked I got
a little more curious and did some more detailed testing including
what happens if the arrays doesn't match in the first few bytes of the
comparison.

These are simple timings. Ratios measured as Unsafe:Safe

match 1000 bytes 1 : 3.9
match 20 bytes 1 : 2.1
match 4 bytes 1 : 1.05
1st byte fail 1 : 0.45
2nd byte fail 1 : 0.6
3rd byte fail 1 : 0.7
4th byte fail 1 : 0.9
5th byte fail 1 : 0.95

When large parts of big arrays has to be compared the difference in
performance approaches 4. This is pretty much expected since the byte
version has to loop and compare 4 times as much.

With byte arrays of size 20 the unsafe version is still twice as fast.
With a really small array, and/or if the comparisons fail in the first
few bytes the advantage turns to the safe version. If the first byte
fails the safe version is more than twice as fast. The overhead of the
unsafe version is too large.

I also can't explain why the "match 4 bytes" differs slightly from
"4th byte fail". From just looking at the code I expected them to
perform the same.

--
Marcus Andrén
Apr 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
5
by: Jim H | last post by:
I will have many 3 byte Byte arrays. Is there a way to compare the values without iterating through and comparing each element in the code? Example: byte lTest1 = new byte {0x1, 0x2, 0x3};...
3
by: NathanV | last post by:
I'm trying to compare two byte's. I think I have to use binary operators. How can I tell if two bytes have the same value? Also, I have a function that is returning a byte value. Is there a...
2
by: nobs | last post by:
Hi I have two byte arrays (each with a length of 8 bytes) an I'd like to compare them if they are equal. The operator == shouldn't work because its not a basic Data Type so I used the method...
1
by: Jacob Thastrup | last post by:
Hi is there an easy way to compare arrays without going through each entry in the arrays? Say I have two arrays and I want to find entries that are not present in both. Thanks Jacob Thastrup
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
8
by: Turbot | last post by:
Hello, Anyone know how to compare two byte arrays without going through each item in the array. I actually want to compare two bitmap objects to see if they both contain the same picture bit...
2
by: Ole | last post by:
How do I compare two byte arrays in a if statement? Thanks Ole
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.