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<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
 */

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值