List: ArrayList, LinkedList
Set: HashSet, TreeSet
Map: HashMap, TreeMap
並且針對了這幾個新增刪除以及取得(Get)來做比較,測試方式是先新增10000個字串物件,然後刪除字串為"5000”的物件,再來則是利用while loop及foreach來展開全部的物件。
實驗環境為
CPU | Intel core i7 860 @2.80GHz |
OS | WIndows 7 X86_64 |
MEM | 6G |
JDK | Java SE 6 Update 23 |
以下為數據結果:
add(push) | delete(remove) | Get(foreach) | Get (while) | |
ArrayList | 10 | 1 | 2 | 4 |
LinkedList | 4 | 1 | 1 | 4 |
HashSet | 13 | 0 | 2 | 5 |
TreeSet | 38 | 0 | 4 | 6 |
HashMap | 3 | 0 | N/A | 4 |
TreeMap | 7 | 0 | N/A | 4 |
程式碼如下
import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.TreeSet; public class PerformanceTest { private static void ArrayListTest(int size){ System.out.println("\nArrayListTest, Total size: "+size); ArrayList<String> list=new ArrayList<String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.add(Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Iterator iterator = list.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); for(String i : list) { s=i; } System.out.println("4.Get time(use enhanced for loop): "+(System.currentTimeMillis()-start)+" (ms)"); } private static void LinkedListTest(int size){ System.out.println("\nLinkedListTest, Total size: "+size); LinkedList<String> list=new LinkedList<String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.add(Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Iterator iterator = list.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); for(String i : list) { s=i; } System.out.println("4.Get time(use enhanced for loop): "+(System.currentTimeMillis()-start)+" (ms)"); } private static void HashSetTest(int size){ System.out.println("\nHashSetTest, Total size: "+size); HashSet<String> list=new HashSet<String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.add(Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Iterator iterator = list.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); for(String i : list) { s=i; } System.out.println("4.Get time(use enhanced for loop): "+(System.currentTimeMillis()-start)+" (ms)"); } private static void TreeSetTest(int size){ System.out.println("\nTreeSetTest, Total size: "+size); TreeSet<String> list=new TreeSet<String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.add(Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Iterator iterator = list.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); for(String i : list) { s=i; } System.out.println("4.Get time(use enhanced for loop): "+(System.currentTimeMillis()-start)+" (ms)"); } private static void HashMapTest(int size){ System.out.println("\nHashMapTest, Total size: "+size); HashMap<String,String> list=new HashMap<String,String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.put(Integer.toString(i),Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Collection collection = list.values(); Iterator iterator = collection.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); } private static void TreeMapTest(int size){ System.out.println("\nTreeMapTest, Total size: "+size); TreeMap<String,String> list=new TreeMap<String,String>(); long start=System.currentTimeMillis(); for(int i=0;i<size;i++){ list.put(Integer.toString(i),Integer.toString(i)); } System.out.println("1.Add time: "+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); list.remove(Integer.toString(size/2)); System.out.println("2.Delete time(delete the object "+size/2+") :"+(System.currentTimeMillis()-start)+" (ms)"); start=System.currentTimeMillis(); Collection collection = list.values(); Iterator iterator = collection.iterator(); String s; while(iterator.hasNext()) { s=(String) iterator.next(); } System.out.println("3.Get time(use iterator): "+(System.currentTimeMillis()-start)+" (ms)"); } public static void main(String[] args) { ArrayListTest(10000); LinkedListTest(10000); HashSetTest(10000); TreeSetTest(10000); HashMapTest(10000); TreeMapTest(10000); } }