C# 三种字节数组(byte[])拼接的性能对比测试

http://blog.csdn.net/sqqyq/article/details/50986977


之前做的通信框架,一直用的List<byte>做的数据接收池。今天有点闲暇时间,特地写了个DEMO将C#中的三种字节数组拼接方式的性能做了一个对比测试。

代码如下(若代码有不严谨或错误之处,恳请指出。微笑):

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Diagnostics;  
  6.   
  7. namespace BytesLinkDemo  
  8. {  
  9.     class Program  
  10.     {  
  11.         static int RunCount = 10000;  
  12.   
  13.         static void Main(string[] args)  
  14.         {  
  15.             ArrayCopyTest();  
  16.             BlockCopyTest();  
  17.             ListTest();  
  18.             Console.ReadKey();  
  19.         }  
  20.   
  21.         static void ListTest()  
  22.         {  
  23.             List<byte> byteSource = new List<byte>();  
  24.             byteSource.Add(11);  
  25.             Stopwatch sw = new Stopwatch();  
  26.             sw.Start();  
  27.             for (int i = 0; i < RunCount; i++)  
  28.             {  
  29.                 byte[] newData = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  
  30.                 byteSource.AddRange(newData);  
  31.             }  
  32.             byte[] data = byteSource.ToArray();  
  33.             //byte[] subData = byteSource.Take(100).ToArray();//获取前100个字节  
  34.             //byteSource.RemoveRange(0, 100);//取出后删除  
  35.             //byteSource.GetRange(100, 100);//从下标100开始取100个字节  
  36.             sw.Stop();  
  37.             Console.WriteLine("ListTest " + sw.ElapsedMilliseconds + " 毫秒,数组长度:" + data.Length);  
  38.         }  
  39.   
  40.         static void ArrayCopyTest()  
  41.         {  
  42.             byte[] byteSource = new byte[1] { 11 };  
  43.             Stopwatch sw = new Stopwatch();  
  44.             sw.Start();  
  45.             for (int i = 0; i < RunCount; i++)  
  46.             {  
  47.                 byte[] newData = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  
  48.                 byte[] tmp = new byte[byteSource.Length + newData.Length];  
  49.                 Array.Copy(byteSource, tmp, byteSource.Length);  
  50.                 Array.Copy(newData, 0, tmp, byteSource.Length, newData.Length);  
  51.                 byteSource = tmp;  
  52.             }  
  53.             sw.Stop();  
  54.             Console.WriteLine("ArrayCopyTest " + sw.ElapsedMilliseconds + " 毫秒,数组长度:" + byteSource.Length);  
  55.         }  
  56.   
  57.         static void BlockCopyTest()  
  58.         {  
  59.             byte[] byteSource = new byte[1] { 11 };  
  60.             Stopwatch sw = new Stopwatch();  
  61.             sw.Start();  
  62.             for (int i = 0; i < RunCount; i++)  
  63.             {  
  64.                 byte[] newData = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  
  65.                 byte[] tmp = new byte[byteSource.Length + newData.Length];  
  66.                 System.Buffer.BlockCopy(byteSource, 0, tmp, 0, byteSource.Length);  
  67.                 System.Buffer.BlockCopy(newData, 0, tmp, byteSource.Length, newData.Length);  
  68.                 byteSource = tmp;  
  69.             }  
  70.             sw.Stop();  
  71.             Console.WriteLine("BlockCopyTest " + sw.ElapsedMilliseconds + " 毫秒,数组长度:" + byteSource.Length);  
  72.         }  
  73.     }  
  74. }  

[csharp]  view plain  copy
 print ?
  1.   

测试结果:


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值