前言

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

那么,前两个章节呢,我们分别讲述了什么时候使用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

相关文章:

  • 2022-12-23
  • 2021-06-30
  • 2021-06-05
  • 2021-06-28
  • 2021-05-31
  • 2021-06-20
  • 2021-07-29
  • 2021-07-22
猜你喜欢
  • 2021-12-20
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
  • 2022-12-23
  • 2021-11-17
相关资源
相似解决方案