Exemplo n.º 1
0
        public unsafe void Test(int iterations)
        {
            Stopwatch sw = new Stopwatch();
            Struct    s  = new Struct(100, 200);
            Class     c;

            System.Threading.Thread.Sleep(3000);
            sw.Start();


            for (int n = 0; n < iterations; n++)
            {
                s.out1 = s.in1 + s.in2;
                s.out2 = s.in2 - s.in1;
            }

            sw.Stop();
            Console.WriteLine("simple struct operation " + sw.ElapsedTicks.ToString());
            sw.Reset();
            System.Threading.Thread.Sleep(3000);
            sw.Start();

            //buffer get/set
            for (int n = 0; n < iterations; n++)
            {
                c = new Class(buffer_in, sizeof(Struct), 0);
                int t1 = c.GetIn1();
                int t2 = c.GetIn2();
                c.SetOut1(t1 + t2);
                c.SetOut2(t2 - t1);
                buffer_out = c.buffer;
            }

            sw.Stop();
            Console.WriteLine("buffer get/set " + sw.ElapsedTicks.ToString());
            sw.Reset();
            System.Threading.Thread.Sleep(3000);
            sw.Start();

            for (int n = 0; n < iterations; n++)
            {
                c = new Class(buffer_in, sizeof(Struct), 0);
                Struct t = c.GetStructure();
                t.out1     = t.in1 + t.in2;
                t.out2     = t.in1 - t.in2;
                c          = new Class(t);
                buffer_out = c.buffer;
            }

            sw.Stop();
            Console.WriteLine("conversion to struct " + sw.ElapsedTicks.ToString());
            sw.Reset();
            System.Threading.Thread.Sleep(3000);
            sw.Start();

            for (int n = 0; n < iterations; n++)
            {
                s          = new Struct();
                s.in1      = BitConverter.ToInt32(buffer_in, 0);
                s.in2      = BitConverter.ToInt32(buffer_in, 4);
                s.out1     = s.in1 + s.in2;
                s.out2     = s.in1 - s.in2;
                buffer_out = new byte[16];
                Buffer.BlockCopy(BitConverter.GetBytes(s.in1), 0, buffer_out, 0, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(s.in2), 0, buffer_out, 4, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(s.out1), 0, buffer_out, 8, 4);
                Buffer.BlockCopy(BitConverter.GetBytes(s.out2), 0, buffer_out, 12, 4);
            }
            sw.Stop();
            Console.WriteLine("classic safe bitconverter " + sw.ElapsedTicks.ToString());
            sw.Reset();
            System.Threading.Thread.Sleep(3000);
            sw.Start();


            for (int n = 0; n < iterations; n++)
            {
                s          = new Struct(buffer_in);
                s.out1     = s.in1 + s.in2;
                s.out2     = s.in1 - s.in2;
                buffer_out = s.frame().buffer;
            }
            sw.Stop();
            Console.WriteLine("template " + sw.ElapsedTicks.ToString());
            sw.Reset();
            System.Threading.Thread.Sleep(3000);
            sw.Start();


            for (int n = 0; n < iterations; n++)
            {
                s          = Inline.Deserialize <Struct>(buffer_in);
                s.out1     = s.in1 + s.in2;
                s.out2     = s.in1 - s.in2;
                buffer_out = Inline.Serialize <Struct>(s);
            }
            sw.Stop();
            Console.WriteLine("inline " + sw.ElapsedTicks.ToString());
        }