zoukankan      html  css  js  c++  java
  • 比较C#中几种常见的复制字节数组方法的效率

    在日常编程过程中,我们可能经常需要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

  • 相关阅读:
    【opencv.js】将图片转换为灰度图
    【快速创建】第一个 opencv.js 项目
    【踩坑无数】Anaconda(2020.02) + Tensorflow2.1 + python3.7 (CPU版本)的安装
    Thread的join方法
    常用语句
    获取当前托管线程的唯一标识符
    修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限(转载)
    C#中Monitor和Lock以及区别(转载)
    LIBRA查询
    Select()和SelectMany()的区别
  • 原文地址:https://www.cnblogs.com/leo-navy/p/4791824.html
Copyright © 2011-2022 走看看