Map四種方法的讀寫效能對比
阿新 • • 發佈:2019-01-23
package lzh; import java.util.HashMap; import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.Map; import java.util.Random; import java.util.TreeMap; import java.util.UUID; /** * 四種常見Map讀寫效能對比 * */ public class TestFourTypeMap { static int hashMapW = 0; static int hashMapR = 0; static int linkMapW = 0; static int linkMapR = 0; static int treeMapW = 0; static int treeMapR = 0; static int hashTableW = 0; static int hashTableR = 0; public static void main(String[] args) { for (int i = 0; i < 10; i++) { TestFourTypeMap test = new TestFourTypeMap(); test.test(100 * 10000); System.out.println(); } System.out.println("hashMapW = " + hashMapW / 10); System.out.println("hashMapR = " + hashMapR / 10); System.out.println("linkMapW = " + linkMapW / 10); System.out.println("linkMapR = " + linkMapR / 10); System.out.println("treeMapW = " + treeMapW / 10); System.out.println("treeMapR = " + treeMapR / 10); System.out.println("hashTableW = " + hashTableW / 10); System.out.println("hashTableR = " + hashTableR / 10); } public void test(int size) { int index; Random random = new Random(); String[] key = new String[size]; // HashMap插入 Map<String, String> map = new HashMap<String, String>(); long start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { key[i] = UUID.randomUUID().toString(); map.put(key[i], UUID.randomUUID().toString()); } long end = System.currentTimeMillis(); hashMapW += (end - start); System.out.println("HashMap插入耗時 = " + (end - start) + " ms"); // HashMap讀取 start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { index = random.nextInt(size); map.get(key[index]); } end = System.currentTimeMillis(); hashMapR += (end - start); System.out.println("HashMap讀取耗時 = " + (end - start) + " ms"); // LinkedHashMap 插入 map = new LinkedHashMap<String, String>(); start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { key[i] = UUID.randomUUID().toString(); map.put(key[i], UUID.randomUUID().toString()); } end = System.currentTimeMillis(); linkMapW += (end - start); System.out.println("LinkedHashMap插入耗時 = " + (end - start) + " ms"); // LinkedHashMap 讀取 start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { index = random.nextInt(size); map.get(key[index]); } end = System.currentTimeMillis(); linkMapR += (end - start); System.out.println("LinkedHashMap讀取耗時 = " + (end - start) + " ms"); // TreeMap 插入 key = new String[size]; map = new TreeMap<String, String>(); start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { key[i] = UUID.randomUUID().toString(); map.put(key[i], UUID.randomUUID().toString()); } end = System.currentTimeMillis(); treeMapW += (end - start); System.out.println("TreeMap插入耗時 = " + (end - start) + " ms"); // TreeMap 讀取 start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { index = random.nextInt(size); map.get(key[index]); } end = System.currentTimeMillis(); treeMapR += (end - start); System.out.println("TreeMap讀取耗時 = " + (end - start) + " ms"); // Hashtable 插入 key = new String[size]; map = new Hashtable<String, String>(); start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { key[i] = UUID.randomUUID().toString(); map.put(key[i], UUID.randomUUID().toString()); } end = System.currentTimeMillis(); hashTableW += (end - start); System.out.println("Hashtable插入耗時 = " + (end - start) + " ms"); // Hashtable 讀取 start = System.currentTimeMillis(); for (int i = 0; i < size; i++) { index = random.nextInt(size); map.get(key[index]); } end = System.currentTimeMillis(); hashTableR += (end - start); System.out.println("Hashtable讀取耗時 = " + (end - start) + " ms"); } } /** HashMap插入耗時 = 8802 ms HashMap讀取耗時 = 493 ms LinkedHashMap插入耗時 = 5197 ms LinkedHashMap讀取耗時 = 395 ms TreeMap插入耗時 = 8213 ms TreeMap讀取耗時 = 3052 ms Hashtable插入耗時 = 4170 ms Hashtable讀取耗時 = 426 ms HashMap插入耗時 = 4683 ms HashMap讀取耗時 = 363 ms LinkedHashMap插入耗時 = 4641 ms LinkedHashMap讀取耗時 = 381 ms TreeMap插入耗時 = 6927 ms TreeMap讀取耗時 = 3020 ms Hashtable插入耗時 = 4028 ms Hashtable讀取耗時 = 406 ms HashMap插入耗時 = 3926 ms HashMap讀取耗時 = 581 ms LinkedHashMap插入耗時 = 4579 ms LinkedHashMap讀取耗時 = 375 ms TreeMap插入耗時 = 6609 ms TreeMap讀取耗時 = 2957 ms Hashtable插入耗時 = 4697 ms Hashtable讀取耗時 = 402 ms HashMap插入耗時 = 3382 ms HashMap讀取耗時 = 353 ms LinkedHashMap插入耗時 = 4271 ms LinkedHashMap讀取耗時 = 375 ms TreeMap插入耗時 = 6366 ms TreeMap讀取耗時 = 2885 ms Hashtable插入耗時 = 4762 ms Hashtable讀取耗時 = 399 ms */