C# 数组Copy的效率问题

来源:互联网 发布:02年韩国黑哨 知乎 编辑:程序博客网 时间:2024/06/05 15:26

在C#中数组Copy是比较常用的,网上有很多帖子,但是写的也不是很完整,今天我对一些比较常用的方法做了一下总结

1、  数组自带的CopyTo方法

private static void NewMethod(byte[] da, byte[] da1)
        {
            Stopwatch dd = new Stopwatch();
            dd.Start();
            for(int i=0;i< tcount;i++)
            {
                da.CopyTo(da1, 0);
            }
            dd.Stop();
            Console.WriteLine("array copyto {0}", dd.ElapsedMilliseconds);
        }

2、 数组的clone方法

  private static void NewMethod1(byte[] da)
        {
            Stopwatch dd = new Stopwatch();
            dd.Start();
            byte[] ddd = null;
            for (int i = 0; i < tcount; i++)
            {
                //ddd=da.Clone() as byte[];
                da.Clone();
            }
            dd.Stop();
            Console.WriteLine("array clone {0}", dd.ElapsedMilliseconds);
        }

3、 Buffer.BlockCopy方法

  private static void NewMethod2(byte[] da, byte[] da1)
        {
            Stopwatch dd = new Stopwatch();
            dd.Start();
            for (int i = 0; i < tcount; i++)
            {
                Buffer.BlockCopy(da, 0, da1, 0, da.Length);
            }
            dd.Stop();
            Console.WriteLine("Buffer.BlockCopy {0}", dd.ElapsedMilliseconds);
        }

4、Array.Copy方法

   private static void NewMethod3(byte[] da, byte[] da1)
        {
            Stopwatch dd = new Stopwatch();
            dd.Start();
            for (int i = 0; i < tcount; i++)
            {
                Array.Copy(da, 0, da1, 0, da.Length);
            }
            dd.Stop();
            Console.WriteLine(" Array.Copy {0}", dd.ElapsedMilliseconds);
        }

5、Array.ConstrainedCopy方法

    private static void NewMethod4(byte[] da, byte[] da1)
        {
            Stopwatch dd = new Stopwatch();
            dd.Start();
            for (int i = 0; i < tcount; i++)
            {
                Array.ConstrainedCopy(da, 0, da1, 0, da.Length);
            }
            dd.Stop();
            Console.WriteLine(" Array.ConstrainedCopy {0}", dd.ElapsedMilliseconds);
        }

6、Marshal.Copy方法

  private static void NewMethod5(byte[] da, byte[] da1)
        {
            Stopwatch dd = new Stopwatch();
            dd.Start();
            //GCHandle handle= GCHandle.Alloc(da1, GCHandleType.Pinned);  //获取数组的Intptr
            //IntPtr des = handle.AddrOfPinnedObject();
            IntPtr des = Marshal.AllocHGlobal(da1.Length);  //获取数组的Intptr
            for (int i = 0; i < tcount; i++)
            {
                Marshal.Copy(da, 0, des, da.Length);
            }
            //handle.Free();
            dd.Stop();
            Console.WriteLine("  Marshal.Copy {0}", dd.ElapsedMilliseconds);
        }

主函数调用

 private const int tcount=10000;    //copy的次数
        static void Main(string[] args)
        {
            string path = @"D:\C#Test\ChatServer\TestUdp\bin\Debug\1.txt";   这是一个1.91M的文本文件
            using (FileStream file = File.Open(path, FileMode.Open))
            {
                byte[] da = new byte[file.Length];
                byte[] da1 = new byte[file.Length];
                byte[] da11 = new byte[file.Length];
                byte[] da12 = new byte[file.Length];
                byte[] da13 = new byte[file.Length];
                byte[] da14 = new byte[file.Length];
                file.Read(da, 0, da.Length);


                
                NewMethod(da, da1);
                NewMethod1(da);
                NewMethod2(da, da11);
                NewMethod3(da, da12);
                NewMethod4(da, da13);
                NewMethod5(da, da14);
            }
            Console.ReadKey();
        }


结果如下:


Marshal.Copy的效率是最高的,接下来是Buffer.Copy(这个方法有网友说是c写的),clone效率是最低的

0 0
原创粉丝点击