还有更好的方法?谁来测试一下 截取复制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++;
}
}
}
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++;
}
}
}
Buffer.BlockCopy 和 Array.Copy的内部实现都是拷贝地址,速度比较快
最后一个unsafe的 感觉上稍微绕了点?
{
bbb[i] = (byte)i;
}效率太低
不如直接Memcopy
{
public static void Main(string[] args)
{
byte[] bbb = new byte[3600000];
byte[] bb = new byte[3600000]; DateTime now = DateTime.Now;
for (int i = 0; i < 3600000; i++)
{
bbb[i] = (byte)i;
}
TimeSpan ts = DateTime.Now - now;
Console.WriteLine("Time is {0}ms", ts.TotalMilliseconds);
}
}
15.6202msclass ClientApp
{
public static void Main(string[] args)
{
byte[] bbb = new byte[3600000];
byte[] bb = new byte[3600000]; DateTime now = DateTime.Now;
Buffer.BlockCopy(bbb, 0, bb, 0, 3600000);
TimeSpan ts = DateTime.Now - now;
Console.WriteLine("Time is {0}ms", ts.TotalMilliseconds);
}
}0ms