472,121 Members | 1,519 Online

# 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 39754
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
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

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
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 discussion thread is closed

Replies have been disabled for this discussion.