Map四种方法的读写性能对比

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 map = new HashMap();
		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();
		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();
		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();
		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
 */

你可能感兴趣的:(Java)