You can use unsafe code to do pointer operations. You can compare the bytes four at a time as integers:
public static bool ArrayCompare(byte[] a, byte[] b) {
if (a.Length != b.Length) return false;
int len = a.Length;
unsafe {
fixed(byte* ap = a, bp = b) {
int* aip = (int*)ap, bip = (int*)bp;
for (;len >= 4;len-=4) {
if (*aip != *bip) return false;
aip++;
bip++;
}
byte* ap2 = (byte*)aip, bp2 = (byte*)bip;
for (;len>0;len--) {
if (*ap2 != *bp2) return false;
ap2++;
bp2++;
}
}
}
return true;
}
A tested this against a simple loop, and it's about six times faster.
As suggested by Josh Einstein, long could be used on a 64 bit system. Actually it seems to be almost twice as fast both on 32 and 64 bit systems:
public static bool ArrayCompare64(byte[] a, byte[] b) {
if (a.Length != b.Length) return false;
int len = a.Length;
unsafe {
fixed (byte* ap = a, bp = b) {
long* alp = (long*)ap, blp = (long*)bp;
for (; len >= 8; len -= 8) {
if (*alp != *blp) return false;
alp++;
blp++;
}
byte* ap2 = (byte*)alp, bp2 = (byte*)blp;
for (; len > 0; len--) {
if (*ap2 != *bp2) return false;
ap2++;
bp2++;
}
}
}
return true;
}