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

将C#中的三种字节数组拼接方式的性能做了一个对比测试,DEMO程序代码如下:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Byte数组拼接测试
{
    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();
            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);
        }
    }

}

测试结果:

我们可以看到每种字节数组拼接方式的执行时间:

  • ArrayCopyTest 使用 Array.Copy() 方法进行拼接,平均耗时约为 73毫秒左右。
  • BlockCopyTest 使用 Buffer.BlockCopy() 方法进行拼接,平均耗时约为 65 毫秒左右。
  • ListTest 使用 List<byte>.AddRange() 方法进行拼接,平均耗时约为 4 毫秒左右。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值