c#byte数组高效复制怎么做
答案:2 悬赏:20 手机版
解决时间 2021-03-04 13:35
- 提问者网友:棒棒糖
- 2021-03-03 17:18
c#byte数组高效复制怎么做
最佳答案
- 五星知识达人网友:不如潦草
- 2021-03-03 18:27
在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IList<T>.Copy,BinaryReader.ReadBytes,Buffer.BlockCopy,以及System.Buffer.memcpyimpl,由于最后一种需要使用指针,所以本文不引入该方法。
本次测试,使用以上前4种方法,各运行1000万次,观察结果。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace BenchmarkCopyArray
{
class Program
{
private const int TestTimes = 10000000;
static void Main()
{
var testArrayCopy = new TestArrayCopy();
TestCopy(testArrayCopy.TestBinaryReader, "Binary.ReadBytes");
TestCopy(testArrayCopy.TestConvertToList, "ConvertToList");
TestCopy(testArrayCopy.TestArrayDotCopy, "Array.Copy");
TestCopy(testArrayCopy.TestBlockCopy, "Buffer.BlockCopy");
Console.Read();
}
private static void TestCopy(Action testMethod, string methodName)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < TestTimes; i++)
{
testMethod();
}
testMethod();
stopWatch.Stop();
Console.WriteLine("{0}: {1} seconds, {2}.", methodName, stopWatch.Elapsed.Seconds, stopWatch.Elapsed.Milliseconds);
}
}
class TestArrayCopy
{
private readonly byte[] _sourceBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
public void TestBinaryReader()
{
var binaryReader = new BinaryReader(new MemoryStream(_sourceBytes));
binaryReader.ReadBytes(_sourceBytes.Length);
}
public void TestConvertToList()
{
IList<byte> bytesSourceList = new List<byte>(_sourceBytes);
var bytesNew = new byte[_sourceBytes.Length];
bytesSourceList.CopyTo(bytesNew, 0);
}
public void TestArrayDotCopy()
{
var bytesNew = new byte[_sourceBytes.Length];
Array.Copy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);
}
public void TestBlockCopy()
{
var bytesNew = new byte[_sourceBytes.Length];
Buffer.BlockCopy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);
}
}
}
本次测试,使用以上前4种方法,各运行1000万次,观察结果。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace BenchmarkCopyArray
{
class Program
{
private const int TestTimes = 10000000;
static void Main()
{
var testArrayCopy = new TestArrayCopy();
TestCopy(testArrayCopy.TestBinaryReader, "Binary.ReadBytes");
TestCopy(testArrayCopy.TestConvertToList, "ConvertToList");
TestCopy(testArrayCopy.TestArrayDotCopy, "Array.Copy");
TestCopy(testArrayCopy.TestBlockCopy, "Buffer.BlockCopy");
Console.Read();
}
private static void TestCopy(Action testMethod, string methodName)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < TestTimes; i++)
{
testMethod();
}
testMethod();
stopWatch.Stop();
Console.WriteLine("{0}: {1} seconds, {2}.", methodName, stopWatch.Elapsed.Seconds, stopWatch.Elapsed.Milliseconds);
}
}
class TestArrayCopy
{
private readonly byte[] _sourceBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
public void TestBinaryReader()
{
var binaryReader = new BinaryReader(new MemoryStream(_sourceBytes));
binaryReader.ReadBytes(_sourceBytes.Length);
}
public void TestConvertToList()
{
IList<byte> bytesSourceList = new List<byte>(_sourceBytes);
var bytesNew = new byte[_sourceBytes.Length];
bytesSourceList.CopyTo(bytesNew, 0);
}
public void TestArrayDotCopy()
{
var bytesNew = new byte[_sourceBytes.Length];
Array.Copy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);
}
public void TestBlockCopy()
{
var bytesNew = new byte[_sourceBytes.Length];
Buffer.BlockCopy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);
}
}
}
全部回答
- 1楼网友:不如潦草
- 2021-03-03 18:33
以下是我写的一个函数,将两个不为空的byte数组合并:
public static byte[] copybyte(byte[] a, byte[] b)
{
byte[] c = new byte[a.length + b.length];
a.copyto(c, 0);
b.copyto(c, a.length);
return c;
}
不知道我理解的对不对,你的a[1]和a[2]都应该是数组吧?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯