C# 如何将fixed与Array或T[]类型的变量一起使用?
我正在开发一个C# 如何将fixed与Array或T[]类型的变量一起使用?,c#,fixed,unsafe,C#,Fixed,Unsafe,我正在开发一个IEqualityComparer,它应该可以非常快速地比较基本类型的数组。我的计划是获取指向数组和memcmp它们的指针。像这样: public unsafe override bool Equals(T[] x, T[] y) { if (ReferenceEquals(x, y)) return true; if (x == null || y == null) return false; if (x.Lengt
IEqualityComparer
,它应该可以非常快速地比较基本类型的数组。我的计划是获取指向数组和memcmp
它们的指针。像这样:
public unsafe override bool Equals(T[] x, T[] y)
{
if (ReferenceEquals(x, y)) return true;
if (x == null || y == null) return false;
if (x.Length != y.Length) return false;
var xArray = (Array)x;
var yArray = (Array)y;
fixed (void* xPtr = xArray) //compiler error 1
fixed (T* yPtr = y) //compiler error 2
{
return memcmp(xPtr, yPtr, x.Length * this.elementSize);
}
}
固定语句不允许我固定Array
或T[]
有以下错误消息:
1. Cannot implicitly convert type 'System.Array' to 'void*'
2. Cannot take the address of, get the size of, or declare a pointer to a managed type ('T')
现在,我并不在乎我是如何做到这一点的(我并不致力于这个确切的方法)。我怎样才能memcmp
twoT[]
知道T
是一种基本类型/可blittable类型
我真的希望避免打开类型,并为每个感兴趣的类型创建专门的(和重复的)代码版本。由于性能限制,任何类型的反射解决方案都是不可行的(是的,我确实需要这里的性能-没有过早的优化警告,因为这是堆栈溢出的常见情况).我同意Daniel A.White的评论,他告诉您,这可能不会带来性能提升,但会因为封送到非托管代码等的开销而导致性能下降 话虽如此,您应该能够使用: 我知道T是一种基本类型/可飞行类型 你知道,编译器不知道。CLR要求固定对象中的所有内容都不能再由垃圾收集器移动。对于数组,它包括其数组元素。唯一符合条件的T类型是一个简单的值类型,一个可blittable的类型。泛型不提供将T约束为可blittable类型的方法 通常将memcmp()的参数声明为字节[]。pinvoke marshaller随后已经做了正确的事情,并将在调用memcmp()之前锁定字节[]数组。但是,这也不起作用,因为您也无法轻松地将T[]转换为字节[]。你得把自己绑在把手上。相应地将memcmp()参数声明为IntPtr,而不是byte[]
可以工作的类型子集在实践中足够小,只考虑编写方法重载而不是泛型方法。它现在使pinvoke封送器能够处理固定,并相应地重载memcmp()函数声明。
您可以为要比较的每种数组编写单独的p/Invoke前端。我知道你不是真的想专门研究t,但我认为开销不是太大 这有点像黑客,因为我为同一个API函数定义了多个具有不同签名的p/Invoke方法,但通过这样做,我可以利用p/Invoke编组支持 (请注意,只有源数据确实是一个字节数组时,memcmp返回值的符号才有意义。如果给它一个int数组,则只应将返回值与零进行比较并忽略其符号。它所暗示的顺序对int没有意义。) 例如,以下代码为我打印以下内容(发布版本,而不是调试版本): 请注意,byte[]测试使用的是字节,因此比较的字节数是int[]测试使用的字节数的四分之一。托管代码进行相同数量的比较,因此速度相对较慢 比较int数组的时间并没有很大差别,但是比较字节数组的时间差别很大。这对我来说意味着可能存在一种管理优化,它使用fixed从字节数组中获取指向int的指针,以便一次比较4个字节(在末尾可能会有一些额外的字节不适合int) 我还认为您可以编写一个多线程托管版本(使用“int*”优化来比较字节数组),它比非托管memcmp()快得多,而非托管memcmp()当然不是多线程的(据我所知) 不管怎样,这是我的测试代码。记住,发布构建,而不是调试using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Demo
{
public static class Program
{
private static void Main(string[] args)
{
int[] a1 = new int[1000000];
int[] a2 = new int[1000000];
for (int i = 0; i < a1.Length-1; ++i)
{
a1[i] = i;
a2[i] = i;
}
a1[a1.Length-1] = 1;
a2[a1.Length-1] = 2;
byte[] b1 = new byte[1000000];
byte[] b2 = new byte[1000000];
for (int i = 0; i < b1.Length-1; ++i)
{
b1[i] = (byte)i;
b2[i] = (byte)i;
}
b1[a1.Length-1] = 1;
b2[a1.Length-1] = 2;
Stopwatch sw = Stopwatch.StartNew();
testWithMemCmp(a1, a2);
sw.Stop();
Console.WriteLine("MemCmp with ints took " + sw.Elapsed);
sw.Restart();
testWithManagedMemCmp(a1, a2);
sw.Stop();
Console.WriteLine("ManagedMemCmp with ints took " + sw.Elapsed);
sw.Restart();
testWithMemCmp(b1, b2);
sw.Stop();
Console.WriteLine("MemCmp with bytes took " + sw.Elapsed);
sw.Restart();
testWithManagedMemCmp(b1, b2);
sw.Stop();
Console.WriteLine("ManagedMemCmp with bytes took " + sw.Elapsed);
}
private static void testWithMemCmp(int[] a1, int[] a2)
{
for (int j = 0; j < COUNT; ++j)
{
MemCmp(a1, a2);
}
}
private static void testWithMemCmp(byte[] a1, byte[] a2)
{
for (int j = 0; j < COUNT; ++j)
{
MemCmp(a1, a2);
}
}
private static void testWithManagedMemCmp(int[] a1, int[] a2)
{
for (int j = 0; j < COUNT; ++j)
{
ManagedMemCmp(a1, a2);
}
}
private static void testWithManagedMemCmp(byte[] a1, byte[] a2)
{
for (int j = 0; j < COUNT; ++j)
{
ManagedMemCmp(a1, a2);
}
}
public static bool ManagedMemCmp(int[] a1, int[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
{
throw new InvalidOperationException("Arrays are null or different lengths.");
}
for (int i = 0; i < a1.Length; ++i)
{
if (a1[i] != a2[i])
{
return false;
}
}
return true;
}
public static bool ManagedMemCmp(byte[] a1, byte[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
{
throw new InvalidOperationException("Arrays are null or different lengths.");
}
for (int i = 0; i < a1.Length; ++i)
{
if (a1[i] != a2[i])
{
return false;
}
}
return true;
}
public static bool MemCmp(byte[] a1, byte[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
{
throw new InvalidOperationException("Arrays are null or different lengths.");
}
return memcmp(a1, a2, new UIntPtr((uint)a1.Length)) == 0;
}
public static bool MemCmp(int[] a1, int[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
{
throw new InvalidOperationException("Arrays are null or different lengths.");
}
return memcmp(a1, a2, new UIntPtr((uint)(a1.Length * sizeof(int)))) == 0;
}
[DllImport("msvcrt.dll")]
private static extern int memcmp(byte[] a1, byte[] a2, UIntPtr count);
[DllImport("msvcrt.dll")]
private static extern int memcmp(int[] a1, int[] a2, UIntPtr count);
private const int COUNT = 10000;
}
}
使用系统;
使用系统诊断;
使用System.Runtime.InteropServices;
名称空间演示
{
公共静态类程序
{
私有静态void Main(字符串[]args)
{
int[]a1=新int[1000000];
int[]a2=新int[1000000];
对于(int i=0;iMemCmp with ints took 00:00:08.0768666
ManagedMemCmp with ints took 00:00:10.3750453
MemCmp with bytes took 00:00:01.8740001
ManagedMemCmp with bytes took 00:00:09.2885763
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Demo
{
public static class Program
{
private static void Main(string[] args)
{
int[] a1 = new int[1000000];
int[] a2 = new int[1000000];
for (int i = 0; i < a1.Length-1; ++i)
{
a1[i] = i;
a2[i] = i;
}
a1[a1.Length-1] = 1;
a2[a1.Length-1] = 2;
byte[] b1 = new byte[1000000];
byte[] b2 = new byte[1000000];
for (int i = 0; i < b1.Length-1; ++i)
{
b1[i] = (byte)i;
b2[i] = (byte)i;
}
b1[a1.Length-1] = 1;
b2[a1.Length-1] = 2;
Stopwatch sw = Stopwatch.StartNew();
testWithMemCmp(a1, a2);
sw.Stop();
Console.WriteLine("MemCmp with ints took " + sw.Elapsed);
sw.Restart();
testWithManagedMemCmp(a1, a2);
sw.Stop();
Console.WriteLine("ManagedMemCmp with ints took " + sw.Elapsed);
sw.Restart();
testWithMemCmp(b1, b2);
sw.Stop();
Console.WriteLine("MemCmp with bytes took " + sw.Elapsed);
sw.Restart();
testWithManagedMemCmp(b1, b2);
sw.Stop();
Console.WriteLine("ManagedMemCmp with bytes took " + sw.Elapsed);
}
private static void testWithMemCmp(int[] a1, int[] a2)
{
for (int j = 0; j < COUNT; ++j)
{
MemCmp(a1, a2);
}
}
private static void testWithMemCmp(byte[] a1, byte[] a2)
{
for (int j = 0; j < COUNT; ++j)
{
MemCmp(a1, a2);
}
}
private static void testWithManagedMemCmp(int[] a1, int[] a2)
{
for (int j = 0; j < COUNT; ++j)
{
ManagedMemCmp(a1, a2);
}
}
private static void testWithManagedMemCmp(byte[] a1, byte[] a2)
{
for (int j = 0; j < COUNT; ++j)
{
ManagedMemCmp(a1, a2);
}
}
public static bool ManagedMemCmp(int[] a1, int[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
{
throw new InvalidOperationException("Arrays are null or different lengths.");
}
for (int i = 0; i < a1.Length; ++i)
{
if (a1[i] != a2[i])
{
return false;
}
}
return true;
}
public static bool ManagedMemCmp(byte[] a1, byte[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
{
throw new InvalidOperationException("Arrays are null or different lengths.");
}
for (int i = 0; i < a1.Length; ++i)
{
if (a1[i] != a2[i])
{
return false;
}
}
return true;
}
public static bool MemCmp(byte[] a1, byte[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
{
throw new InvalidOperationException("Arrays are null or different lengths.");
}
return memcmp(a1, a2, new UIntPtr((uint)a1.Length)) == 0;
}
public static bool MemCmp(int[] a1, int[] a2)
{
if (a1 == null || a2 == null || a1.Length != a2.Length)
{
throw new InvalidOperationException("Arrays are null or different lengths.");
}
return memcmp(a1, a2, new UIntPtr((uint)(a1.Length * sizeof(int)))) == 0;
}
[DllImport("msvcrt.dll")]
private static extern int memcmp(byte[] a1, byte[] a2, UIntPtr count);
[DllImport("msvcrt.dll")]
private static extern int memcmp(int[] a1, int[] a2, UIntPtr count);
private const int COUNT = 10000;
}
}