测试四种Map的读写性能(hashMap,linkMap,treeMap,hashTable)

package com.lewai.map;
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 FourMap {
	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++) {
			FourMap test = new FourMap();
			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插入耗时 = 8280 ms

HashMap读取耗时 = 321 ms

LinkedHashMap插入耗时 = 6528 ms

LinkedHashMap读取耗时 = 317 ms

TreeMap插入耗时 = 7138 ms

TreeMap读取耗时 = 1649 ms

Hashtable插入耗时 = 6426 ms

Hashtable读取耗时 = 287 ms

 

HashMap插入耗时 = 6041 ms

HashMap读取耗时 = 265 ms

LinkedHashMap插入耗时 = 5889 ms

LinkedHashMap读取耗时 = 259 ms

TreeMap插入耗时 = 7759 ms

TreeMap读取耗时 = 2031 ms

Hashtable插入耗时 = 6318 ms

Hashtable读取耗时 = 267 ms

 

HashMap插入耗时 = 5253 ms

HashMap读取耗时 = 260 ms

LinkedHashMap插入耗时 = 6430 ms

LinkedHashMap读取耗时 = 289 ms

TreeMap插入耗时 = 7243 ms

TreeMap读取耗时 = 1733 ms

Hashtable插入耗时 = 5180 ms

Hashtable读取耗时 = 297 ms

 

HashMap插入耗时 = 5166 ms

HashMap读取耗时 = 268 ms

LinkedHashMap插入耗时 = 6181 ms

LinkedHashMap读取耗时 = 258 ms

TreeMap插入耗时 = 6575 ms

TreeMap读取耗时 = 1689 ms

Hashtable插入耗时 = 5877 ms

Hashtable读取耗时 = 286 ms

 

HashMap插入耗时 = 5393 ms

HashMap读取耗时 = 278 ms

LinkedHashMap插入耗时 = 6056 ms

LinkedHashMap读取耗时 = 267 ms

TreeMap插入耗时 = 7101 ms

TreeMap读取耗时 = 1683 ms

Hashtable插入耗时 = 6194 ms

Hashtable读取耗时 = 285 ms

 

HashMap插入耗时 = 5210 ms

HashMap读取耗时 = 274 ms

LinkedHashMap插入耗时 = 6257 ms

LinkedHashMap读取耗时 = 301 ms

TreeMap插入耗时 = 6695 ms

TreeMap读取耗时 = 1651 ms

Hashtable插入耗时 = 5760 ms

Hashtable读取耗时 = 287 ms

 

HashMap插入耗时 = 5673 ms

HashMap读取耗时 = 282 ms

LinkedHashMap插入耗时 = 5569 ms

LinkedHashMap读取耗时 = 287 ms

TreeMap插入耗时 = 7077 ms

TreeMap读取耗时 = 1761 ms

Hashtable插入耗时 = 5117 ms

Hashtable读取耗时 = 279 ms

 

HashMap插入耗时 = 5287 ms

HashMap读取耗时 = 272 ms

LinkedHashMap插入耗时 = 6069 ms

LinkedHashMap读取耗时 = 310 ms

TreeMap插入耗时 = 6706 ms

TreeMap读取耗时 = 1662 ms

Hashtable插入耗时 = 6423 ms

Hashtable读取耗时 = 276 ms

 

HashMap插入耗时 = 5299 ms

HashMap读取耗时 = 277 ms

LinkedHashMap插入耗时 = 5288 ms

LinkedHashMap读取耗时 = 264 ms

TreeMap插入耗时 = 7225 ms

TreeMap读取耗时 = 1738 ms

Hashtable插入耗时 = 5330 ms

Hashtable读取耗时 = 283 ms

 

HashMap插入耗时 = 5460 ms

HashMap读取耗时 = 276 ms

LinkedHashMap插入耗时 = 6040 ms

LinkedHashMap读取耗时 = 265 ms

TreeMap插入耗时 = 6471 ms

TreeMap读取耗时 = 1670 ms

Hashtable插入耗时 = 5237 ms

Hashtable读取耗时 = 290 ms

 

hashMapW = 5706

hashMapR = 277

linkMapW = 6030

linkMapR = 281

treeMapW = 6999

treeMapR = 1726

hashTableW = 5786

hashTableR = 283

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值