program.cs

来自「Reading packets over the network with di」· CS 代码 · 共 121 行

CS
121
字号
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ReadingStructureData
{
    struct Packet
    {
        public short Source;
        public short Destination;
        public int Checksum;
        public int Identifier;
        public int SecondaryIdentifier;
    }

    class Program
    {
        static Packet ReadUsingBinaryReader(byte[] data)
        {
            Packet packet;
            using (BinaryReader reader = new BinaryReader(new MemoryStream(data, false)))
            {
                packet.Source = reader.ReadInt16();
                packet.Destination = reader.ReadInt16();
                packet.Checksum = reader.ReadInt32();
                packet.Identifier = reader.ReadInt32();
                packet.SecondaryIdentifier = reader.ReadInt32();
            }
            return packet;
        }

        static Packet ReadUsingPointer(byte[] data)
        {
            unsafe
            {
                fixed (byte* packet = &data[0])
                {
                    return *(Packet*)packet;
                }
            }
        }

        static T ReadUsingMarshalSafe<T>(byte[] data) where T : struct
        {
            GCHandle gch = GCHandle.Alloc(data, GCHandleType.Pinned);
            try
            {
                return (T)Marshal.PtrToStructure(gch.AddrOfPinnedObject(), typeof(T));
            }
            finally
            {
                gch.Free();
            }
        }

        static T ReadUsingMarshalUnsafe<T>(byte[] data) where T : struct
        {
            unsafe
            {
                fixed (byte* p = &data[0])
                {
                    return (T) Marshal.PtrToStructure(new IntPtr(p), typeof(T));
                }
            }
        }

        static T ReadUsingCppCli<T>(byte[] data) where T : struct
        {
            return GenericRead.Reader.Read<T>(data);
        }

        static void Main(string[] args)
        {
            byte[] data = { 1, 0, 2, 0, 5, 0, 0, 0, 6, 0, 0, 0, 8, 0, 0, 0 };
            Stopwatch stopwatch;

            Console.WriteLine("Non-Generic Solutions:" + Environment.NewLine);
            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; ++i)
            {
                ReadUsingBinaryReader(data);
            }
            Console.WriteLine("BinaryReader: " + stopwatch.ElapsedMilliseconds);

            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; ++i)
            {
                ReadUsingPointer(data);
            }
            Console.WriteLine("Pointer: " + stopwatch.ElapsedMilliseconds);

            Console.WriteLine(Environment.NewLine + Environment.NewLine +
                "Generic Solutions:" + Environment.NewLine);
            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; ++i)
            {
                ReadUsingMarshalSafe<Packet>(data);
            }
            Console.WriteLine("MarshalSafe: " + stopwatch.ElapsedMilliseconds);

            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; ++i)
            {
                ReadUsingMarshalUnsafe<Packet>(data);
            }
            Console.WriteLine("MarshalUnsafe: " + stopwatch.ElapsedMilliseconds);

            stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < 1000000; ++i)
            {
                ReadUsingCppCli<Packet>(data);
            }
            Console.WriteLine("C++/CLI: " + stopwatch.ElapsedMilliseconds);
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?