当前位置: 移动技术网 > IT编程>开发语言>c# > 浅谈C#各种数组直接的数据复制/转换

浅谈C#各种数组直接的数据复制/转换

2019年07月18日  | 移动技术网IT编程  | 我要评论
之前做opengl程序,用的的c#的sharpgl这个库,里面有各种奇怪绑定的函数,比如原型为: void glinterleavedarrays(uint fo

之前做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 适用场合最广泛,效率最高。

以上这篇浅谈c#各种数组直接的数据复制/转换就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网