常用数据集Array、ArrayList、LinkedList、HashMap各种常用遍历花费时间比较

     本人初学者,如有不合理的地方,还行各位路过的大侠们帮忙拍砖。

     这段时间在看集合这一章,书上说Array的效率会比集合的效率高,按理说也是理所应当的。自己也就动手写了个测试类,测一测Array 及通用集合的遍历效率,结果,测试出来的数据让我很是惊讶。

  这次测试并没有把Array的效率体现出来,仅仅是从遍历相同的数据所花费的时间来比较的,。

  List在java1.5版本中添加的foreach循环,在List遍历中是最常使用的,花费的时间紧随Iterator。这让我疑惑,foreach的底层实现是不是用的是Iterator?。

      在ArrayList中,for循环遍历的成绩让我最为惊异,花费的时间最少。

  

控制台输出数据如下:

 1 ================================ Array =================================
 2 Array(for)--->47
 3 Array([])--->0
 4 ================================ ArrayList =================================
 5 ArrayList(foreach)--->6
 6 ArrayList(for)--->2
 7 ArrayList(Iterator)--->5
 8 ArrayList([99999])--->0
 9 ================================ LinkedList =================================
10 LinkedList(foreach)--->5
11 LinkedList(for)--->71586
12 LinkedList(Iterator)--->4
13 LinkedList([99999])--->0
14 ================================ HashMap =================================
15 HashMap(Iterator)--->7
16 HashMap(.get("99999"))--->0

 

代码如下:

  1 package com.wondersgroup.hs.test.c10;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.Iterator;
  6 import java.util.LinkedList;
  7 import java.util.List;
  8 import java.util.Map;
  9 
 10 
 11 
 12 /**
 13  * 比较Array、ArrayList、LinkedList、HashMap的查询速度比较
 14  * @author JingYuan
 15  *
 16  */
 17 public class TheSpendofQuery {
 18     public static void main(String[] args)
 19     {
 20         queryArray();
 21         queryArrayList();
 22         queryLinkedList();
 23         queryHashMap();
 24     }
 25     
 26     /**
 27      * 测试数组查询速度
 28      */
 29     public static void queryArray()
 30     {
 31         
 32         //构造Array,大小为100000
 33         Person[] personArray = new Person[100000];
 34         //为数组填充数据
 35         for(Integer i=0;i<100000;i++)
 36         {
 37             personArray[i]= new Person(i.toString(),i.toString());
 38         }
 39         
 40         
 41         //以99999为例
 42         print("================================ Array =================================");
 43         Long end = null;
 44         String str = null;
 45         Long result = null;
 46         Long start = System.currentTimeMillis();
 47         
 48         for(Integer i=0;i<100000;i++)
 49         {
 50             if(personArray[i].getName().equals("99999"))
 51             {
 52                 str = personArray[i].getAge();
 53                 end = System.currentTimeMillis();
 54                 break;
 55             }
 56         }
 57         result = end - start;
 58         print("\nArray(for)--->" + result);
 59         
 60         
 61         str = null;
 62         start = null;
 63         end = null;
 64         result = null;
 65         start = System.currentTimeMillis();
 66         str = personArray[99999].getAge();
 67         end = System.currentTimeMillis();
 68         result = end -start;
 69         print("\nArray([])--->" + result);
 70     }
 71     /**
 72      * 测试ArrayList查询速度
 73      */
 74     public static void queryArrayList()
 75     {
 76         print("\n================================ ArrayList =================================");
 77         //构造ArrayList
 78         List<Person> list = new ArrayList<Person>();
 79         for(Integer i = 0;i<100000;i++)
 80         {
 81             list.add(new Person(i.toString(),i.toString()));
 82         }
 83         //1.foreach
 84         Long end = null;
 85         String str = null;
 86         Long result = null;
 87         Long start = System.currentTimeMillis();
 88         for(Person p:list)
 89         {
 90             if("99999".equals(p.getName()))
 91             {
 92                 str = p.getAge();
 93                 end = System.currentTimeMillis();
 94                 break;
 95             }
 96         }
 97         print("\nArrayList(foreach)--->"+(end-start));
 98         //for
 99         end = null;
100         str = null;
101         result = null;
102         start = System.currentTimeMillis();
103         for(Integer i=0;i<100000;i++)
104         {
105             if("99999".equals(list.get(i).getName()))
106             {
107                 str = list.get(i).getAge();
108                 end = System.currentTimeMillis();
109                 break;
110             }
111         }
112         print("\nArrayList(for)--->"+(end-start));
113         //Iterator
114         end = null;
115         str = null;
116         result = null;
117         Person tmp = null;
118         start = System.currentTimeMillis();
119         for(Iterator i = list.iterator();i.hasNext();)
120         {
121             tmp = (Person)i.next();
122             if("99999".equals(tmp.getName()))
123             {
124                 str = tmp.getAge();
125                 end = System.currentTimeMillis();
126                 break;
127             }
128         }
129         print("\nArrayList(Iterator)--->"+(end-start));
130         //[99999]
131         end = null;
132         str = null;
133         result = null;
134         tmp = null;
135         start = System.currentTimeMillis();
136         tmp = list.get(99999);
137         end = System.currentTimeMillis();
138         print("\nArrayList([99999])--->"+(end-start));
139         
140     }
141     /**
142      * 测试LinkedList
143      */
144     public static void queryLinkedList()
145     {
146         print("\n================================ LinkedList =================================");
147         //构造集合
148         List<Person> list = new LinkedList<Person>();
149         for(Integer i=0;i<100000;i++)
150         {
151             list.add(new Person(i.toString(),i.toString()));
152         }
153         
154         //1.foreach
155         Long end = null;
156         String str = null;
157         Long result = null;
158         Long start = System.currentTimeMillis();
159         for(Person p:list)
160         {
161             if("99999".equals(p.getName()))
162             {
163                 str = p.getAge();
164                 end = System.currentTimeMillis();
165                 break;
166             }
167         }
168         print("\nLinkedList(foreach)--->"+(end-start));
169         //for
170         end = null;
171         str = null;
172         result = null;
173         start = System.currentTimeMillis();
174         
175         for(Integer i=0;i<100000;i++)
176         {
177             if("99999".equals(list.get(i).getName()))
178             {
179                 str = list.get(i).getAge();
180                 end = System.currentTimeMillis();
181                 break;
182             }
183         }
184         print("\nLinkedList(for)--->"+(end-start));
185         //Iterator
186         end = null;
187         str = null;
188         result = null;
189         Person tmp = null;
190         start = System.currentTimeMillis();
191         for(Iterator i = list.iterator();i.hasNext();)
192         {
193             tmp = (Person)i.next();
194             if("99999".equals(tmp.getName()))
195             {
196                 str = tmp.getAge();
197                 end = System.currentTimeMillis();
198                 break;
199             }
200         }
201         print("\nLinkedList(Iterator)--->"+(end-start));
202         //[99999]
203         end = null;
204         str = null;
205         result = null;
206         tmp = null;
207         start = System.currentTimeMillis();
208         tmp = list.get(99999);
209         end = System.currentTimeMillis();
210         print("\nLinkedList([99999])--->"+(end-start));
211         
212     }
213     /**
214      * 测试HashMap
215      */
216     public static void queryHashMap()
217     {
218         print("\n================================ HashMap =================================");
219         //构造集合
220         Map<String,Person> map = new HashMap<String,Person>();
221         for(Integer i=0;i<100000;i++)
222         {
223             map.put(i.toString(), new Person(i.toString(),i.toString()));
224         }
225         //Iterator
226         Long start = null;
227         Long end = null;
228         Person tmp = null;
229         start = System.currentTimeMillis();
230         for(Iterator i = map.keySet().iterator();i.hasNext();)
231         {
232             tmp = map.get(i.next());
233             if(tmp.getName().equals("99999"))
234             {
235                 end = System.currentTimeMillis();
236                 break;
237             }
238         }
239         print("\nHashMap(Iterator)--->"+(end-start));
240         //[99999]
241         start = null;
242         end = null;
243         tmp = null;
244         start = System.currentTimeMillis();
245         tmp = map.get("99999");
246         end = System.currentTimeMillis();
247         print("\nHashMap(.get(\"99999\"))--->"+(end-start));
248         
249     }
250     /**
251      * 打印
252      * @param str
253      */
254     public static void print(String str)
255     {
256         System.out.print(str);
257     }
258 }
259 
260 /**
261  * 测试中使用的类
262  * @author JingYuan
263  *
264  */
265 class Person{
266     private  String name;
267     private  String age;
268     public Person(String name,String age)
269     {
270         this.name = name;
271         this.age = age;
272     }
273     public String getName() {
274         return name;
275     }
276     public void setName(String name) {
277         this.name = name;
278     }
279     public String getAge() {
280         return age;
281     }
282     public void setAge(String age) {
283         this.age = age;
284     }
285     
286     
287     
288 }

 

posted @ 2012-08-06 10:50  十面埋伏  阅读(380)  评论(0)    收藏  举报