规则集Set与线性表List性能分析

前言

  本章节将通过实验,测试规则集与线性表的性能。那么如何进行实验呢?针对不同的集合都进行指定数量元素的添加和删除操作,计算耗费时间进行分析。

那么,前两个章节呢,我们分别讲述了什么时候使用Set以及List中的实现类效率最高。下面贴出链接,方便查看:

《规则集之探究何时使用HashSet、LinkedHashSet以及TreeSet?》

《线性表之何时使用ArrayList、LinkedList?》

 代码实现区:

 1 package collection;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Collection;
 5 import java.util.Collections;
 6 import java.util.HashSet;
 7 import java.util.LinkedHashSet;
 8 import java.util.LinkedList;
 9 import java.util.List;
10 import java.util.TreeSet;
11 
12 /**
13  * 
14  * Title: SetListPerformerTest Description:规则集和线性表性能比对
15  * 
16  * @author yacong_liu Email:2682505646@qq.com
17  * @date 2017年9月13日下午11:41:19
18  */
19 public class SetListPerformerTest {
20     private static final List<Integer> COL_LIST = new ArrayList<Integer>();
21     private static final int COL_SIZE = 50000;
22 
23     static {
24         for (int i = 0; i < COL_SIZE; i++) {
25             COL_LIST.add(i);
26         }
27     }
28 
29     public static void main(String[] args) {
30 
31         /****** start Set ********/
32         Collection<Integer> setHash = new HashSet<Integer>();
33             System.out.println("HashSet time is " + getTestTime(setHash, COL_SIZE) + "ms");
34         Collection<Integer> setLinkedHash = new LinkedHashSet<Integer>();
35             System.out.println("LinkedHashSet time is " + getTestTime(setLinkedHash, COL_SIZE) + "ms");
36         Collection<Integer> setTree = new TreeSet<Integer>();
37             System.out.println("TreeSet time is " + getTestTime(setTree, COL_SIZE) + "ms");
38         /****** end Set ********/
39 
40         /****** start List ********/
41         Collection<Integer> listArray = new ArrayList<Integer>();
42             System.out.println("ArrayList time is " + getTestTime(listArray, COL_SIZE) + "ms");
43         Collection<Integer> listLinked = new LinkedList<Integer>();
44             System.out.println("LinkedList time is " + getTestTime(listLinked, COL_SIZE) + "ms");
45         /****** end List ********/
46 
47     }
48 
49     /**
50      * 
51      * Title: getTestTime Description: 计算集合中 添加元素 和 删除元素所需耗时 以此来进行测试不同的集合
52      * 插入删除元素时性能
53      * 
54      * @author yacong_liu Email:2682505646@qq.com
55      * @date 2017年9月13日下午11:59:40
56      * @param col
57      * @param size
58      * @return
59      */
60     private static long getTestTime(Collection<Integer> col, int size) {
61         long sTime = System.currentTimeMillis();
62 
63         // 打乱线性表COL_LIST
64         Collections.shuffle(COL_LIST);
65 
66         // 往集合中添加元素
67         for (Integer element : COL_LIST) {
68             col.add(element);
69         }
70 
71         Collections.shuffle(COL_LIST);
72 
73         // 从集合中删除元素
74         for (Integer element : COL_LIST) {
75             col.remove(element);
76         }
77 
78         long eTime = System.currentTimeMillis();
79         return eTime - sTime;
80     }
81 
82 }
View Code

Console输出结果:

1 HashSet time is 52ms
2 LinkedHashSet time is 58ms
3 TreeSet time is 245ms
4 ArrayList time is 1806ms
5 LinkedList time is 3631ms

实验结果分析:

  描述:当我切换不同的COL_SIZE大小的时候,会出现明显的性能情况,5000输出的时候还是挺快的,但是改为500000的时候,只有Setd的实现类快速输出打印了,List实现类一直没有打印输出结果,运行的很慢。其中 HashSet 两种情况下打印输出都是最快的!

  结果:

    1. 规则集Set比线性表List更加的高效! 因此,可以使用规则集的时候,就使用规则集Set。

    2. 使用规则集的时候,如果不需要考虑特别的顺序,那就选择散列集HashSet。

    3. ArrayList和LinkedList 进行删除操作时,复杂度基本一致。注意:线性表中除结尾以外的任意位置上的插入删除元素操作,链式线性表比数组线性表表现的更为高效。

原文地址:https://www.cnblogs.com/lyc-smile/p/7518525.html