之前做OpenGL程序,用的的C#的SharpGL這個庫,裡面有各種奇怪綁定的函數,比如原型為:
void glInterleavedArrays(uint format, int stride, void * pointer);
的函數被他綁定成:
private static extern void glInterleavedArrays(uint format, int stride, int[] pointer);
然後我就被逼著學習了各種float[] 轉 int[] 的方法,比較他們的效率(其實我還是感覺c++比較快,一個指針類型轉換,歐啦)
下面是我寫的各種數組賦值轉換的方法和結果對比。
1.Marshal.Copy,存在數組到IntPtr,IntPtr到數組的2次拷貝【當T2不是Copy支持的類型會出錯,之所以引入dynamic dTo 是因為使用T2[] dTo 無法編譯通過】,處理2000000*100字節1120.0018ms
public static T2[] Arr2Arr<T1, T2>(T1[] from)
where T1: struct
where T2 :struct
{
int byteNum = from.Length * from[0].Sizeof();
T2 testByte = new T2();
dynamic dFrom = from;
dynamic dTo = new T2[byteNum / testByte.Sizeof()];
IntPtr ptr = Marshal.AllocHGlobal(byteNum);
Marshal.Copy(dFrom, 0, ptr, from.Length);
Marshal.Copy(ptr, dTo, 0, dTo.Length);
return dTo;
}
2.unsafe的方法,通過指針獲得IntPtr,減少了一次復制,速度變快【當T2不是Copy支持的類型會出錯,之所以引入pFrom是因為無法fixed泛型T1[]】,處理2000000*100字節695.9993ms
public unsafe static T2[] Arr2Arr<T1, T2>(T1[] from, void * pFrom)
where T1 : struct
where T2 : struct
{
int byteNum = from.Length * from[0].Sizeof();
T2 testByte = new T2();
dynamic dTo = new T2[byteNum / testByte.Sizeof()];
IntPtr ptr = new IntPtr(pFrom);
Marshal.Copy(ptr, dTo, 0, dTo.Length);
return dTo;
}
3.通過GCHandle獲得IntPtr,然後復制【當T2不是Copy支持的類型會出錯】,處理2000000*100字節930.0481ms
public static T2[] Arr2Arr2<T1, T2>(T1[] from)
where T1 : struct
where T2 : struct
{
var gch = GCHandle.Alloc(from,GCHandleType.Pinned);
IntPtr ptr = gch.AddrOfPinnedObject();
int byteNum = from.Length * from[0].Sizeof();
T2 testByte = new T2();
dynamic dTo = new T2[byteNum / testByte.Sizeof()];
Marshal.Copy(ptr, dTo, 0, dTo.Length);
gch.Free();
return dTo;
}
4.Array.Copy的方法,原生的數組復制方法【沒有了Copy,可以處理任意值類型】,處理2000000*100字節620.042ms
public static T2[] Arr2Arr3<T1, T2>(T1[] from)
where T1 : struct
where T2 : struct
{
int byteNum = from.Length * from[0].Sizeof();
T2 testByte = new T2();
T2[] dTo = new T2[byteNum / testByte.Sizeof()];
Array.Copy(from, dTo, dTo.Length);
return dTo;
}
5.通過Buffer.BlockCopy拷貝數組,速度最快,感覺類似於c++的memcpy【沒有了Copy,可以處理任意值類型】,處理2000000*100字節300.0329ms
public static T2[] Arr2Arr4<T1, T2>(T1[] from)
where T1 : struct
where T2 : struct
{
int byteNum = from.Length * from[0].Sizeof();
T2 testByte = new T2();
T2[] dTo = new T2[byteNum / testByte.Sizeof()];
Buffer.BlockCopy(from, 0, dTo, 0, byteNum);
return dTo;
}
測試部分代碼:
byte[] from = new byte[100];
from[0] = 1;
from[1] = 1;
var last = DateTime.Now;
for (int i = 0; i < 2000000; i++)
{
。。。
}
Console.WriteLine((DateTime.Now- last).TotalMilliseconds);
//sizeof擴展方法
internal static class ExFunc
{
public static int Sizeof(this ValueType t)
{
return Marshal.SizeOf(t);
}
}
綜上所述,Buffer.BlockCopy 適用場合最廣泛,效率最高。
例子到Linux公社資源站下載:
------------------------------------------分割線------------------------------------------
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2016年資料/3月/10日/C#各種數組直接的數據復制轉換/
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
------------------------------------------分割線------------------------------------------