zoukankan      html  css  js  c++  java
  • C#把某个数组的一部分复制到另一个数组中的两种方法:Buffer.BlockCopy和Array.Copy

    static void Main(string[] args)
            {
                int[] src = new[] { 1, 2, 3, 4, 5, 6 };
                const int destLen = 4;//目标数组大小
                int int_size = sizeof(int);//用于获取值类型的字节大小。
                int[] dest = new int[destLen];
                //只支持基元类型,按字节偏移复制
                Buffer.BlockCopy(src, (src.Length - destLen) * int_size, dest, 0, destLen * int_size);
                foreach (var i in dest)
                {
                    Console.Write(i + "  ");
                }
                Console.WriteLine("
    -------------------------------------------");
                string[] srcstr = new[] { "A", "B", "C", "D", "E", "F" };
                object[] destobj = new object[src.Length - 2];
                //移除的元素个数
                const int dellen = 2;
                //保证不破坏目标数组的元素(回滚)。不装箱、拆箱、或向下转换,否则报错。
               //如果srcstr改为src则报错,因为装箱。
                Array.ConstrainedCopy(srcstr, dellen, destobj, 0, srcstr.Length - dellen);
                foreach (var s in destobj)
                {
                    Console.Write(s + "  ");
                }
            }

    对指定数组和目标数组,两者类型一致的前提下,进行复制10亿次,

    消耗时间如下:

    copy:59.374s,constrainecopy:48.415 s,blockcopy:23.219s

    代码没什么就是测试下,核心测试如下:

     int[] ints = { 1534, 233, 332, 423, 524, 3246, 4357, 734, 567, 43, 34254, 325, 3325, 2423, 345, 575, 235, 1, 342, 1, 6, 54645, 5432, 5 };
     int[] dest = new int[ints.Length];

    Array.Copy(ints, dest, ints.Length);

    Array.ConstrainedCopy(ints, 0, dest, 0, ints.Length);

    Buffer.BlockCopy(ints, 0, dest, 0, ints.Length * 4);

    注解分析:

    1,Array.Copy在CLR处理机制中最灵活,最强大,可装箱,拆箱复制,可加宽CLR基元类型,可内部判断实现了IFarmattable接口的兼容转换,当然这种强大方式必然会带来一定的性能损失。

    2,Array.ConstrainedCopy 对复制要求严格,只能是同类型或者源数组类型是目标类型的派生元素类型,不执行装箱,拆箱,向下转换

    3,Buffer.BlockCopy 则从本质上以字节为复制单位,这在底层语言C,C++的处理优势上,同理,效率之高可以理解。

    当然如果对性能要求不高,Copy足矣,毕竟在上千次复制下,三者基本没消耗多少时间。使用时可根据项目需求斟酌选择!

    问题:c#如何把某个长数组的一部分复制到另一个短数组里面

    byte[] shortAry=new byte[4];
    byte[] longAry=new byte[20];
    如何把longAry[5,9(不含)]这4个字节复制到shortAry里面?
    不要用循环。

    用Array.Copy方法将数组或者数组的一部分复制到另个数组。Array.Copy是静态方法,有多个重载版本。其中常用的是:

    public static void Copy(
        Array sourceArray,
        int sourceIndex,
        Array destinationArray,
        int destinationIndex,
        int length);

    各个参数含义如下

    • sourceArray —— 源数组

    • sourceIndex —— 表示 sourceArray 中复制开始处的索引

    • destinationArray —— 目标数组,它接收数据

    • destinationIndex —— 表示 destinationArray 中存储开始处的索引

    • length —— 要复制的元素数目。

    用法举例如下:

    (1)复制数组的一部分到另一个数组

    int[] src = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    int[] dest = new int[4];
    // 将数组 src 中元素 2,3,4,5 复制到 dest  
    Array.Copy(src, 1, dest, 0, 4);

    (2)复制整个数组

    int[] src = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    int[] dest = new int[src.Length];
    // 将数组 src 所有元素复制到 dest  
    Array.Copy(src, 0, dest, 0, src.Length);

     其它参考链接:C#数据结构-Array.Copy和Buffer.BlockCopy详解

  • 相关阅读:
    opencv入门踩坑之路(一)
    编写vue的时候(html也一样),限制一个div内可显示的字数
    win10开机后内存占用非常高,但是资源管理器显示的进程占用得很少,可以考虑更新驱动
    java 随机数产生 常用类及方法
    你不会的是这个正则表达式吗?
    系统编程第三次上机
    Java第三次上机随笔
    系统编程第二次实验
    Java第二次上机随笔
    面向对象-Java MOOC翁恺老师第一次作业
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/5812872.html
Copyright © 2011-2022 走看看