首页 新闻 会员 周边

为什么array比queue,linkedlist,list都在快,而且性能最差的竟然是linkedlist

0
[待解决问题]

为什么array(string[]) 的速度比别的都要快呢,想不明白,说好的时间复制度,在这里怎么感觉不对,不知道哪里理解出错了.

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Collections;
  6 using System.Diagnostics;
  7 
  8 namespace ConsoleApplication1
  9 {
 10     class Program
 11     {
 12         static void Main(string[] args)
 13         {
 14             Stopwatch  sw=new Stopwatch ();
 15 
 16 
 17             #region
 18             sw.Restart();
 19             LinkedList<string> ll = new LinkedList<string>();
 20             for (int i = 0; i < 50000; i++)
 21             {
 22                 ll.AddLast("sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
 23             }
 24             int sdf = ll.Count;
 25             string d = null;
 26             for (int i = 0; i < sdf; i++)
 27             {
 28                 d = ll.First();
 29                 ll.RemoveFirst();
 30             }
 31             sw.Stop();
 32             Console.WriteLine(  sw.Elapsed.Ticks.ToString()+"--LinkedList");
 33             #endregion 
 34 
 35             #region 
 36             sw.Restart();
 37             Queue q = new Queue();
 38 
 39             for (int i = 0; i < 50000; i++)
 40             {
 41                 q.Enqueue("sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
 42             }
 43             int qCount= q.Count;
 44             string s = null;
 45             object o = null;
 46             for (int i = 0; i < qCount; i++)
 47             {
 48                 s = (string)q.Dequeue();
 49                 //o = q.Dequeue();
 50             }
 51             sw.Stop();
 52             Console.WriteLine(sw.Elapsed.Ticks.ToString() + "--Queue:");
 53 
 54             #endregion 
 55 
 56             #region
 57             sw.Restart();
 58             string[] arr = new string[50000];
 59 
 60             
 61             //Queue q = new Queue();
 62             int ddd = 50000;
 63             for (int i = 0; i < ddd; i++)
 64             {
 65                 arr[i] = "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss";
 66             }
 67            
 68             //int qCount = q.Count;
 69             //string s = null;
 70             object o3 = null;
 71             for (int i = 0; i < qCount; i++)
 72             {
 73                 o3 = arr[i];
 74                 //o = q.Dequeue();
 75             }
 76             sw.Stop();
 77             Console.WriteLine(sw.Elapsed.Ticks.ToString()+"--array:");
 78 
 79             #endregion 
 80 
 81             #region
 82             sw.Restart();
 83             IList<string> ls = new List<string>();
 84 
 85             //Queue q = new Queue();
 86 
 87             for (int i = 0; i < 50000; i++)
 88             {
 89                 ls.Add( "sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss");
 90             }
 91 
 92             //int qCount = q.Count;
 93             //string s = null;
 94 
 95             int lsLen =ls.Count();
 96             object o4 = null;
 97             for (int i = 0; i < lsLen; i++)
 98             {
 99                 o4 = ls[i];
100                 //o = q.Dequeue();
101             }
102             sw.Stop();
103             Console.WriteLine(sw.Elapsed.Ticks.ToString() + "--List");
104 
105             #endregion 
106 
107             Console.ReadKey();
108             Console.ReadLine(); 
109            
110 
111         }
112     }
113 }

我是猴子的主页 我是猴子 | 菜鸟二级 | 园豆:210
提问于:2014-10-29 17:44
< >
分享
所有回答(1)
0

时间复制度是个什么东西?是时间复杂度把?想知道你这个问题答案,去吧你这个例子里所有调用的方法看下是怎么实现的就知道了。

吴瑞祥 | 园豆:29449 (高人七级) | 2014-10-30 09:06

链表:

添加或者删除,最后一个或者最前一个

链表的时间复制度不是O(1)么,

虽然每个对象占用内存多,也应该很快啊;

-------------

数组:

要使用索引定位到某一个对象,才能对其操作.

O(n)

支持(0) 反对(0) 我是猴子 | 园豆:210 (菜鸟二级) | 2014-10-30 09:16

@我是猴子: 你往里面添加的是一个引用,而不是具体的值,占用内存并不多的。

重要的是你掉用的那些方法执行了什么操作。你自己反编译看一下

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2014-10-30 11:13
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册