C# 字節數組拼接的速度實驗(Array.copy(),Buffer.BlockCopy(),Contact())


無聊做了如題的一個算法的優劣性能比較,由於很多人都只關心結果,那么我先貼出結果如下:

由於我的測試數據量比較小,只能得出Array.Copy()和Buffer.BlockCopy()方法性能要好於Contact(),這個不用比較也能想到,如果想知道前兩個誰的性能更好,

有興趣的可以修改源碼中的測試數據量就可以了。

測試源碼如下:

        static int len1 = 1470;
        static int len2 = 906;
        static byte[] bytes0 = new byte[len1];
        static byte[] bytes1 = new byte[len2];
        static void Main(string[] args)
        {
            // Uses the second Core or Processor for the Test
            Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(1);
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            // Prevents "Normal" Threads 
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            Stopwatch sw = new Stopwatch();
            byte[] resultBytes = null;
            InitBytes();

            //test
            sw.Reset();
            sw.Start();
            while (sw.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 mS 
                                                   // stabilizes the CPU cache and pipeline.
            {
                resultBytes = Contact(bytes0, bytes1);       // Warmup
            }
            sw.Stop();
            for (int i = 0; i < 10; i++)
            {
                sw.Reset();
                sw.Start();
                resultBytes = Contact(bytes0, bytes1);
                sw.Stop();
                Console.WriteLine("Contact Ticks: {0} Time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
            }
            sw.Reset();
            sw.Start();
            while (sw.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 mS 
                                                   // stabilizes the CPU cache and pipeline.
            {
                resultBytes = BufferCopy(bytes0, bytes1);       // Warmup
            }
            sw.Stop();
            for (int i = 0; i < 10; i++)
            {
                sw.Reset();
                sw.Start();
                resultBytes = BufferCopy(bytes0, bytes1);
                sw.Stop();
                Console.WriteLine("BufferCopy Ticks: {0} Time: {1} ms", sw.ElapsedTicks, sw.ElapsedMilliseconds);
            }
            sw.Reset();
            sw.Start();
            while (sw.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 mS 
                                                   // stabilizes the CPU cache and pipeline.
            {
                resultBytes = ArrayCopy(bytes0, bytes1);       // Warmup
            }
            sw.Stop();
            for (int i = 0; i < 10; i++)
            {
                sw.Reset();
                sw.Start();
                resultBytes = ArrayCopy(bytes0, bytes1);
                sw.Stop();
                Console.WriteLine("ArrayCopy Ticks: {0} Time: {1} ms ", sw.ElapsedTicks, sw.ElapsedMilliseconds);
            }
            Console.ReadKey();
        }
        static void InitBytes()
        {
            for(int i=0;i<len1;i++)
            {
                bytes0[i] =(byte)(i % 255);
            }
            for (int i = 0; i < len2; i++)
            {
                bytes1[i] = (byte)(i % 255);
            }
        }
        static byte[] Contact(byte[] bytes0,byte[] bytes1)
        {
            return bytes0.Concat(bytes1).ToArray();
        }
        static byte[] BufferCopy(byte[] bytes0,byte[] bytes1)
        {
            byte[] resultBytes = new byte[bytes0.Length+bytes1.Length];
            Buffer.BlockCopy(bytes0, 0, resultBytes, 0, bytes0.Length);
            Buffer.BlockCopy(bytes1, 0, resultBytes, bytes0.Length, bytes1.Length);
            return resultBytes;
        }
        static byte[] ArrayCopy(byte[] bytes0, byte[] bytes1)
        {
            byte[] resultBytes = new byte[bytes0.Length + bytes1.Length];
            Array.Copy(bytes0, 0, resultBytes, 0, bytes0.Length);
            Array.Copy(bytes1, 0, resultBytes, bytes0.Length, bytes1.Length);
            return resultBytes;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM