C#數組複製效能測試

來源:互聯網
上載者:User

標籤:ring   data   name   測試結果   gen   star   lib   png   nbsp   

 

 1 using System; 2 using System.Collections; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Diagnostics; 6 using System.IO; 7 using System.Linq; 8 using System.Runtime.InteropServices; 9 using System.Threading;10 using EmisData;11 using Newtonsoft.Json;12 13 namespace CLibrary.ConsoleApp14 {15     class Program16     {17         static void Main(string[] args)18         {19             int len = 50000000;20             int[] newArray = new int[len];21             Stopwatch sw = new Stopwatch();22             int[] allArray = new int[len];23             for (int i = 0; i < len; i++)24             {25                 allArray[i] = i;26             }27 28             sw.Start();29             Thread.Sleep(1000);30             Array.Copy(allArray, newArray, len);31             sw.Stop();32             Console.WriteLine("Array.Copy 執行時間(毫秒)"+ sw.ElapsedMilliseconds);33 34             Array.Clear(newArray, 0, len);35             sw.Restart();36             Thread.Sleep(1000);37             newArray = (int[])allArray.Clone();38             sw.Stop();39             Console.WriteLine("Array.Clone 執行時間(毫秒)" + sw.ElapsedMilliseconds);40 41             Array.Clear(newArray, 0, len);42             sw.Restart();43             Thread.Sleep(1000);44             Buffer.BlockCopy(allArray, 0, newArray, 0, len);45             sw.Stop();46             Console.WriteLine("Buffer.BlockCopy 執行時間(毫秒)" + sw.ElapsedMilliseconds);47 48             Console.ReadKey();49         }50 51        52 53     }54 55 }

測試結果:

 可見:Buffer.BlockCopy 表現最佳

C#數組複製效能測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.