HashMap与ConcurrentHashMap性能对比

目的是测试HashMap与ConcurrentHashMap性能

package com.test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

/**
* HashMap与ConcurrentHashMap性能对比 <br/>
* 在相同条件下分别插入、遍历10W、50W、100W、200W、500W 数据 <br/>
* 记录遍历每种不同数据的耗时<br/>
* 2011-08-30
*
* @author http://www.laoz.net
*
*/
public class TestHashMap {
private static Map<Integer,String> hashMap = new HashMap<Integer,String>();
private static Map<Integer,String> cMap = new ConcurrentHashMap<Integer,String>();

/**
* 测试hashMap put数据
* @param num
*/
public static void testhashMapPut(int num){
long beginTime = System.currentTimeMillis();
for(int i=0;i<num;i++){
hashMap.put(i, “test”);
}
System.out.println(“HashMap :put耗时” + (System.currentTimeMillis()-beginTime) + “ms”);
}

/**
* 测试concurrentHashMap put数据
* @param num
*/
public static void testCMapPut(int num){
long beginTime = System.currentTimeMillis();
for(int i=0;i<num;i++){
cMap.put(i, “test”);
}
System.out.println(“ConcurrentHashMap :put耗时” + (System.currentTimeMillis()-beginTime) + “ms”);
}

/**
* 测试HashMap
*/
public static void testHashMap(){
long beginTime = System.currentTimeMillis();
for(Integer id:hashMap.keySet()){
hashMap.get(id);
}
System.out.println(“HashMap :get耗时” + (System.currentTimeMillis()-beginTime) + “ms”);
}

/**
* 测试 ConcurrentHashMap
*/
public static void testConcurrentHashMap(){
long beginTime = System.currentTimeMillis();
for(Integer id:cMap.keySet()){
cMap.get(id);
}
System.out.println(“ConcurrentHashMap :get耗时” + (System.currentTimeMillis()-beginTime) + “ms”);
}

public static void main(String[] args) {
System.out.println(“测试10W数据”);
testhashMapPut(100000);
testHashMap();
testCMapPut(100000);
testConcurrentHashMap();
System.out.println(“==============================”);

System.out.println(“测试50W数据”);
testhashMapPut(500000);
testHashMap();
testCMapPut(500000);
testConcurrentHashMap();
System.out.println(“==============================”);

System.out.println(“测试100W数据”);
testhashMapPut(1000000);
testHashMap();
testCMapPut(1000000);
testConcurrentHashMap();
System.out.println(“==============================”);

System.out.println(“测试200W数据”);
testhashMapPut(2000000);
testHashMap();
testCMapPut(2000000);
testConcurrentHashMap();
System.out.println(“==============================”);

System.out.println(“测试500W数据”);
testhashMapPut(5000000);
testHashMap();
testCMapPut(5000000);
testConcurrentHashMap();
System.out.println(“==============================”);
}
}

 

测试结果:

测试10W数据
HashMap :put耗时16ms
HashMap :get耗时0ms
ConcurrentHashMap :put耗时46ms
ConcurrentHashMap :get耗时16ms
==============================
测试50W数据
HashMap :put耗时125ms
HashMap :get耗时16ms
ConcurrentHashMap :put耗时250ms
ConcurrentHashMap :get耗时47ms
==============================
测试100W数据
HashMap :put耗时187ms
HashMap :get耗时47ms
ConcurrentHashMap :put耗时500ms
ConcurrentHashMap :get耗时109ms
==============================
测试200W数据
HashMap :put耗时750ms
HashMap :get耗时94ms
ConcurrentHashMap :put耗时1703ms
ConcurrentHashMap :get耗时235ms
==============================
测试500W数据
HashMap :put耗时2093ms
HashMap :get耗时219ms
ConcurrentHashMap :put耗时2906ms
ConcurrentHashMap :get耗时719ms
==============================

 

结论: 结论:无论是put还是get,ConcurrentHashMap耗时约是HashMap的2~3倍

HashMap与ConcurrentHashMap性能对比》上有2条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注