dimanche 4 octobre 2015

can i use this same as with byte to other types values / objects?

As my goal is to out perform the List<T> i am testing arrays and found few starting points to get on testing i have tested this before trying to capture bitmaps off screen, and tests proved the usage is suffice. my question is what data types could use this Copy() code except for byte[]

say i want a data storage unit to take the advantage of unmanaged / unsafe

        public unsafe struct NusT
        {
            public unsafe int vi;
            public unsafe bool vb;
        }

instead of populating a list

i initialise the struct as follows : 1)

NusT n; 
n.vi= 90;
n.vb=true

i have tested this after testing the folowing: 2)

NusT n = new NusT(){vi=90, vb=true};

this test was after testing :3)

NusT n = new NusT("90", true);

i think both last had same results but the first one is blazing fast, as i do not create an object so

NusT n-> instructions- 1
n.vi=90 -> instructions- 1
n.vb=true -> instructions- 1 

now i minimized what i could and this started at the begining with a class: whitch was even worse than 2 & 3 above as it also uses properties

class bigAndSlow
{
     public int a { get; private set;}
     public bool b { get; private set;}
     public string  c { get; private set;}

     public bigAndSlow(int .. ,boo .. , string.. )
     {
        initialise ...
     }
}

so now when the final decision is

        public unsafe struct NusT
        {
            public unsafe int vi;
            public unsafe bool vb;
        }

how can i implement this blazingly fast data unit to use Copy() on

NusT[] NustyArr;


    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();
            }


            // 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 n = 0; n < count / 4; n++)
                {
                    *((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 n = 0; n < count % 4; n++)
                {
                    *pd = *ps;
                    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");
        }

Aucun commentaire:

Enregistrer un commentaire