zoukankan      html  css  js  c++  java
  • C# 三种字节数组(byte[])拼接的性能对比测试

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

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

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
     
    namespace BytesLinkDemo
    {
        class Program
        {
            static int RunCount = 10000;
     
            static void Main(string[] args)
            {
                ArrayCopyTest();
                BlockCopyTest();
                ListTest();
                Console.ReadKey();
            }
     
            static void ListTest()
            {
                List<byte> byteSource = new List<byte>();
                byteSource.Add(11);
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < RunCount; i++)
                {
                    byte[] newData = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                    byteSource.AddRange(newData);
                }
                byte[] data = byteSource.ToArray();
                //byte[] subData = byteSource.Take(100).ToArray();//获取前100个字节
                //byteSource.RemoveRange(0, 100);//取出后删除
                //byteSource.GetRange(100, 100);//从下标100开始取100个字节
                sw.Stop();
                Console.WriteLine("ListTest " + sw.ElapsedMilliseconds + " 毫秒,数组长度:" + data.Length);
            }
     
            static void ArrayCopyTest()
            {
                byte[] byteSource = new byte[1] { 11 };
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < RunCount; i++)
                {
                    byte[] newData = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                    byte[] tmp = new byte[byteSource.Length + newData.Length];
                    Array.Copy(byteSource, tmp, byteSource.Length);
                    Array.Copy(newData, 0, tmp, byteSource.Length, newData.Length);
                    byteSource = tmp;
                }
                sw.Stop();
                Console.WriteLine("ArrayCopyTest " + sw.ElapsedMilliseconds + " 毫秒,数组长度:" + byteSource.Length);
            }
     
            static void BlockCopyTest()
            {
                byte[] byteSource = new byte[1] { 11 };
                Stopwatch sw = new Stopwatch();
                sw.Start();
                for (int i = 0; i < RunCount; i++)
                {
                    byte[] newData = new byte[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                    byte[] tmp = new byte[byteSource.Length + newData.Length];
                    System.Buffer.BlockCopy(byteSource, 0, tmp, 0, byteSource.Length);
                    System.Buffer.BlockCopy(newData, 0, tmp, byteSource.Length, newData.Length);
                    byteSource = tmp;
                }
                sw.Stop();
                Console.WriteLine("BlockCopyTest " + sw.ElapsedMilliseconds + " 毫秒,数组长度:" + byteSource.Length);
            }
        }
    }
    

      

    测试结果:


    JAVA&NET技术QQ群号:456257217有问题的可以在群里面提问。
  • 相关阅读:
    OpenCV学习笔记(一)
    scrapy学习笔记一
    Mac上使用selenium自动运行chrome
    【js Utils】web前端工具帮助类kikyoUtils
    【jq 分享】伪微信分享
    【Winform 动图】winform窗体显示动图
    【 Base<T> 】IBaseDao 和 IBaseService 通用 基类 实现
    【js 是否手机】JavaScript判读当前是否是手机端
    【Spring helper】在controller和service间添加业务处理helper类
    【 spring resources 】maven项目resources文件夹,配置文件的spring加载方式
  • 原文地址:https://www.cnblogs.com/shiyh/p/15797504.html
Copyright © 2011-2022 走看看