C# 通过指针实现的fastcopy的代码

把开发过程中常用的内容备份一次,下面代码是关于C# 通过指针实现的fastcopy的代码。

using System;

class Test
{
    static unsafe void Copy(byte[] src, int srcIndex,
        byte[] dst, int dstIndex, int count)
    {
        if (src == null || srcIndex < 0 ||
            dst == null || dstIndex < 0 || count < 0)
        {
            throw new ArgumentException();
        }
        int srcLen = src.Length;
        int dstLen = dst.Length;
        if (srcLen - srcIndex < count ||
            dstLen - dstIndex < count)
        {
            throw new ArgumentException();
        }


        {

            for (int n = 0; n < count / 4; n++)
            {
                pd += 4;
                ps += 4;
            }

            for (int n = 0; n < count % 4; n++)
            {
                pd++;
                ps++;
            }
        }
    }


    static void Main(string[] args)
    {
        byte[] a = new byte[100];
        byte[] b = new byte[100];
        for (int i = 0; i < 100; ++i)
            a[i] = (byte)i;
        Copy(a, 0, b, 0, 100);
        Console.WriteLine("The first 10 elements are:");
        for (int i = 0; i < 10; ++i)
            Console.Write(b[i] + " ");
        Console.WriteLine("n");
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43947759/article/details/84789126