码迷,mamicode.com
首页 > 编程语言 > 详细

C#数组复制性能测试

时间:2017-08-10 17:01:10      阅读:168      评论:0      收藏:0      [点我收藏+]

标签: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.ConsoleApp
14 {
15     class Program
16     {
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#数组复制性能测试

标签:ring   data   name   测试结果   gen   star   lib   png   nbsp   

原文地址:http://www.cnblogs.com/yy1234/p/7339732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!