C#字节数组比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1389570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# byte array comparison
提问by George2
I have two byte arrays in C# using .NET 3.0.
我在 C# 中有两个字节数组,使用 .NET 3.0。
What is the "most efficient" way to compare whether the two byte arrays contains the same content for each element?
比较两个字节数组是否包含每个元素的相同内容的“最有效”方法是什么?
For example, byte array {0x1, 0x2}
is the same as {0x1, 0x2}
. But byte array {0x1, 0x2}
and byte array {0x2, 0x1}
are not the same.
例如,字节数组{0x1, 0x2}
与{0x1, 0x2}
. 但是字节数组{0x1, 0x2}
和字节数组{0x2, 0x1}
不一样。
采纳答案by Jon Skeet
Well, you could use:
那么,你可以使用:
public static bool ByteArraysEqual(byte[] b1, byte[] b2)
{
if (b1 == b2) return true;
if (b1 == null || b2 == null) return false;
if (b1.Length != b2.Length) return false;
for (int i=0; i < b1.Length; i++)
{
if (b1[i] != b2[i]) return false;
}
return true;
}
(I normally use braces for everything, but I thought I'd experiment with this layout style just for a change...)
(我通常对所有东西都使用大括号,但我想我会尝试这种布局样式只是为了改变......)
This has a few optimisations which SequenceEqual
can't (or doesn't) perform - such as the up-front length check. Direct array access will also be a bit more efficient than using the enumerator.
这有一些SequenceEqual
不能(或不)执行的优化 - 例如预先长度检查。直接数组访问也比使用枚举器更有效。
Admittedly it's unlikely to make a significant difference in most cases...
诚然,在大多数情况下它不太可能产生重大影响......
You could possiblymake it faster in unmanaged code by making it compare 32 or 64 bits at a time instead of 8 - but I wouldn't like to code that on the fly.
你可以有可能通过使其在一个时间,而不是8比32或64位,使其更快非托管代码-但我不希望代码在运行。
回答by LukeH
You can use the SequenceEqual
method:
您可以使用以下SequenceEqual
方法:
bool areEqual = firstArray.SequenceEqual(secondArray);
As mentioned in the comments, SequenceEqual
requires .NET 3.5 (or LINQBridgeif you're using VS2008 and targeting an earlier version of the framework).
如评论中所述,SequenceEqual
需要 .NET 3.5(如果您使用的是 VS2008 并针对早期版本的框架,则需要LINQBridge)。
回答by Philippe Leybaert
If you want it to be really fast, you can use unsafe code (which isn't always possible):
如果你希望它非常快,你可以使用不安全的代码(这并不总是可行的):
public static bool ArraysEqual(byte[] b1, byte[] b2)
{
unsafe
{
if (b1.Length != b2.Length)
return false;
int n = b1.Length;
fixed (byte *p1 = b1, p2 = b2)
{
byte *ptr1 = p1;
byte *ptr2 = p2;
while (n-- > 0)
{
if (*ptr1++ != *ptr2++)
return false;
}
}
return true;
}
}
回答by Guffa
Jon mentioned comparing multiple bytes at once using unsafe code, so I had to give it a go:
Jon 提到使用不安全代码一次比较多个字节,所以我不得不试一试:
public unsafe bool ByteArraysEqual(byte[] b1, byte[] b2) {
if (b1 == b2) return true;
if (b1 == null || b2 == null) return false;
if (b1.Length != b2.Length) return false;
int len = b1.Length;
fixed (byte* p1 = b1, p2 = b2) {
int* i1 = (int*)p1;
int* i2 = (int*)p2;
while (len >= 4) {
if (*i1 != *i2) return false;
i1++;
i2++;
len -= 4;
}
byte* c1 = (byte*)i1;
byte* c2 = (byte*)i2;
while (len > 0) {
if (*c1 != *c2) return false;
c1++;
c2++;
len--;
}
}
return true;
}
The safe code gets pretty optimised (the compiler knows that it doesn't have to check index boundaries for example), so I wouldn't expect the unsafe code to be very much faster. Any significant difference would come from the ability to compare several bytes at once.
安全代码得到了相当的优化(例如,编译器知道它不必检查索引边界),所以我不期望不安全代码会快得多。任何显着差异都来自一次比较多个字节的能力。
回答by LCJ
If you are not too concerned about performance, you can consider IStructuralEquatable
.
如果不是太在意性能,可以考虑IStructuralEquatable
。
.NET Framework Supported in: 4.5, 4
.NET Framework 受以下版本支持:4.5、4
Structural equality means that two objects are equal because they have equal values. It differs from reference equality.
结构相等意味着两个对象相等,因为它们具有相等的值。它不同于引用相等。
Example:
例子:
static bool ByteArrayCompare(byte[] a1, byte[] a2)
{
IStructuralEquatable eqa1 = a1;
return eqa1.Equals(a2, StructuralComparisons.StructuralEqualityComparer);
}
REFERENCE
参考