【发布时间】:2023-03-31 02:42:01
【问题描述】:
我在不同的地方读到过,关于使用固定块的数组之间的不安全迭代有多快。我在 .net 4 和 4.5 中尝试过,或多或少地达到了相同的结果。
安全比较总是更快,有时是一点点,有时几乎是一半,特别是在 .net 4 中。
我做错了吗?
class Program
{
public unsafe static int UnsafeCompareTo2(byte[] self, byte[] other)
{
if (self.Length < other.Length) { return -1; }
if (self.Length > other.Length) { return +1; }
GCHandle selfHandle =
GCHandle.Alloc(self, GCHandleType.Pinned);
GCHandle otherHandle =
GCHandle.Alloc(other, GCHandleType.Pinned);
byte* selfPtr = (byte*)
selfHandle.AddrOfPinnedObject().ToPointer();
byte* otherPtr = (byte*)
otherHandle.AddrOfPinnedObject().ToPointer();
int length = self.Length;
int comparison = 0;
for (int index = 0; index < length; index++)
{
comparison =
(*selfPtr++).CompareTo((*otherPtr++));
if (comparison != 0) { break; }
}
selfHandle.Free();
return comparison;
}
public static int CompareTo(byte[] self, byte[] other)
{
if (self.Length < other.Length) { return -1; }
if (self.Length > other.Length) { return +1; }
int comparison = 0;
for (int i = 0; i < self.Length && i < other.Length; i++)
{
if ((comparison = self[i].CompareTo(other[i])) != 0)
{ return comparison; }
}
return comparison;
}
public unsafe static int UnsafeCompareTo(byte[] self, byte[] other)
{
if (self.Length < other.Length) { return -1; }
if (self.Length > other.Length) { return +1; }
int n = self.Length;
fixed (byte* selfPtr = self, otherPtr = other)
{
byte* ptr1 = selfPtr;
byte* ptr2 = otherPtr;
while (n-- > 0)
{
int comparison;
if ((comparison = (*ptr1++).CompareTo(*ptr2++)) != 0)
{
return comparison;
}
}
}
return 0;
}
static void Main(string[] args)
{
byte[] b1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
byte[] b2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21 };
Stopwatch watch = new Stopwatch();
watch.Start();
int result;
for(long i = 0; i < Math.Pow(10, 2); i++)
result = CompareTo(b1, b2);
watch.Stop();
Console.WriteLine("safe = {0}", watch.Elapsed);
watch.Restart();
for (long i = 0; i < Math.Pow(10, 2); i++)
result = UnsafeCompareTo(b1, b2);
watch.Stop();
Console.WriteLine("unsafe1 = {0}", watch.Elapsed);
watch.Restart();
for (long i = 0; i < Math.Pow(10, 2); i++)
result = UnsafeCompareTo2(b1, b2);
watch.Stop();
Console.WriteLine("unsafe2 = {0}", watch.Elapsed);
Console.ReadLine();
}
}
【问题讨论】:
-
可能是比较的装箱/拆箱,我得试试。有趣的 +1
-
有趣的是,在 if 中取出长度验证,有时第二个不安全的比较会获胜......哈哈
-
我尝试了更多迭代,10^5,然后 unsafe 更快
-
是的,我虽然这将是一个线性进展,但在 100000 次迭代的顺序中,第一个不安全显示执行比较的时间有一半,即使它有 if´s
-
几乎所有时间,半毫秒...我已经尝试了 10^3 次迭代,保险箱更快,但 10^5 显示了每个人都在谈论的性能提升...