测试一下截取byte[]的效率
还有更好的方法?谁来测试一下 截取复制byte[] 的效率。
byte[] bbb = new byte[360000];
byte[] bb = new byte[360000];
for (int i = 0; i < 360000; i++)
{
bbb[i] = (byte)i;
}
bbb复制给bb
Buffer.BlockCopy(bbb, 0, bb, 0, 360000); //系统自带的方法
Copy.(bbb, 0, bb, 0, 360000); //另一个查到的方法最下面写的方法
看看效率差多少。都测试一下,给100分,呵呵
Load Time: ?
Load Time: ?
/// <summary>
/// 使用指针复制字节数组
/// </summary>
public static void Copy(ref byte[] src, int srcIndex, ref byte[] dst, int dstIndex, int count)
{
if (src == null || srcIndex < 0 ||
dst == null || dstIndex < 0 || count < 0)
{
throw new System.ArgumentException();
}
int srcLen = src.Length;
int dstLen = dst.Length;
if (srcLen - srcIndex < count || dstLen - dstIndex < count)
{
throw new System.ArgumentException();
}
// The following fixed statement pins the location of the src and dst objects
// in memory so that they will not be moved by garbage collection.
fixed (byte* pSrc = src, pDst = dst)
{
byte* ps = pSrc;
byte* pd = pDst;
// Loop over the count in blocks of 4 bytes, copying an integer (4 bytes) at a time:
for (int i = 0; i < count / 4; i++)
{
*((int*)pd) = *((int*)ps);
pd += 4;
ps += 4;
}
// Complete the copy by moving any bytes that weren't moved in blocks of 4:
for (int i = 0; i < count % 4; i++)
{
*pd = *ps;
pd++;
ps++;
}
}
}