比较C#中几种常见的复制字节数组方法的效率

Posted on 2015-09-08 15:20  小木屋  阅读(1142)  评论(0编辑  收藏  举报

在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IList<T>.Copy,BinaryReader.ReadBytes,Buffer.BlockCopy,以及System.Buffer.memcpyimpl,由于最后一种需要使用指针,所以本文不引入该方法。 

         本次测试,使用以上前4种方法,各运行1000万次,观察结果。

 

[csharp] view plaincopy
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Diagnostics;  
  4. using System.IO;  
  5.   
  6. namespace BenchmarkCopyArray  
  7. {  
  8.     class Program  
  9.     {  
  10.         private const int TestTimes = 10000000;  
  11.         static void Main()  
  12.         {  
  13.             var testArrayCopy = new TestArrayCopy();  
  14.             TestCopy(testArrayCopy.TestBinaryReader, "Binary.ReadBytes");  
  15.             TestCopy(testArrayCopy.TestConvertToList, "ConvertToList");  
  16.             TestCopy(testArrayCopy.TestArrayDotCopy, "Array.Copy");  
  17.             TestCopy(testArrayCopy.TestBlockCopy, "Buffer.BlockCopy");  
  18.             Console.Read();  
  19.         }  
  20.   
  21.         private static void TestCopy(Action testMethod, string methodName)  
  22.         {  
  23.             var stopWatch = new Stopwatch();  
  24.             stopWatch.Start();  
  25.             for (int i = 0; i < TestTimes; i++)  
  26.             {  
  27.                 testMethod();  
  28.             }  
  29.             testMethod();  
  30.             stopWatch.Stop();  
  31.             Console.WriteLine("{0}: {1} seconds, {2}.", methodName, stopWatch.Elapsed.Seconds, stopWatch.Elapsed.Milliseconds);  
  32.         }  
  33.     }  
  34.   
  35.     class TestArrayCopy  
  36.     {  
  37.         private readonly byte[] _sourceBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  
  38.   
  39.         public void TestBinaryReader()  
  40.         {  
  41.             var binaryReader = new BinaryReader(new MemoryStream(_sourceBytes));  
  42.             binaryReader.ReadBytes(_sourceBytes.Length);  
  43.         }  
  44.   
  45.         public void TestConvertToList()  
  46.         {  
  47.             IList<byte> bytesSourceList = new List<byte>(_sourceBytes);  
  48.             var bytesNew = new byte[_sourceBytes.Length];  
  49.             bytesSourceList.CopyTo(bytesNew, 0);  
  50.         }  
  51.   
  52.         public void TestArrayDotCopy()  
  53.         {  
  54.             var bytesNew = new byte[_sourceBytes.Length];  
  55.             Array.Copy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);  
  56.         }  
  57.   
  58.         public void TestBlockCopy()  
  59.         {  
  60.             var bytesNew = new byte[_sourceBytes.Length];  
  61.             Buffer.BlockCopy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);  
  62.         }  
  63.     }  
  64. }  


       运行结果如下:

 

       希望以上测试对您会有所帮助。

转自:http://blog.csdn.net/jiangzhanchang/article/details/9998229

Copyright © 2024 小木屋
Powered by .NET 8.0 on Kubernetes