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倍

文章来源于网络或者作者投稿,若有侵权请联系删除,作者:老钟,如若转载,请注明出处:https://www.laoz.net/122.html

(0)
上一篇 2011 年 08 月 30 日
下一篇 2011 年 08 月 30 日

相关推荐

  • windows下部署JAVA项目

    写了一个程序,在eclipse下运行速度挺快的,但是内存消耗太多了,就eclipse这个进程就消耗了200M的内存,再加上java程序的内存消耗,感觉2G的内存有点吃不消!所以,要减少eclipse的内存支出,把java项目直接通过...

    闲话杂谈 2012 年 10 月 21 日
  • 想转SAP FICO顾问的必看 (转)

    早就答应给大家写一下我做SAP顾问的一点心得,今日稍稍得闲,想起对诸位的承诺,不如早早动笔,免得日久忘笔食言。 想来做这个行业时间不长,但感触颇深,在此分享,希望对诸位有所裨益! 分以下几个话题: 1. ...

    闲话杂谈 2012 年 02 月 26 日
  • java 日期与毫秒相互转化

    因需要,所以写了个日期转化成毫秒,把毫秒转化成日期的类 import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** * 测试时间 * 把毫秒转换成 yyyy-MM-dd HH:mm:ss * @au...

    闲话杂谈 2011 年 08 月 22 日
  • iphone 验机

    你拿到机子查看sn码是否一致,就是机盒背面的sn序列号与卡槽上的sn序列号和手机-设置-通用-关于本机 里 面的序列号一样,如果有电脑,连接电脑,itunes上出现的序列号也应该一致。在有条件就用电脑上网,登陆 htt...

    闲话杂谈 2011 年 08 月 08 日
  • 在URLHttpConnection中使用代理服务器

    原文出处: 在URLHttpConnection中使用代理服务器 作者: Jet Mah from Java堂 在JDK5之前如果在URLHttpConnection中使用代理服务器的话,只要在URL.openConnection()之前加入以下代码就可以: Properties prop = S...

    闲话杂谈 2011 年 08 月 25 日
  • 日语,50音

    偶尔一个机会,需要整理一下日语50音(中文应该是音标)   あ(a) い(i) う(u) え(e) お(o) か(ka) き(ki) く(ku) け(ke) こ(ko) さ(sa) し(shi) す(su) せ(se) そ(so) た(ta) ち(chi) つ(tsu) て(te) と(to)...

    闲话杂谈 2012 年 03 月 01 日
  • 天猫客服应该注意的事项,转自互联网

    网上看到的,本身自己也想收集一些规则,然后归纳总结出来的,既然网上有了,就直接拿过来用,出处忘记是哪里了,如果下次找到再更新文章!天猫不同于集市,淘宝对于天猫商城店铺管理要比C店严格的多了,很多时候...

    闲话杂谈 2013 年 08 月 19 日
  • Eclipse中导入工程提示:invalid project description 解决办法

    打开工程目录下的 .project文件, 把<name>标签中的文字改成自己的工程名

    闲话杂谈 2012 年 02 月 08 日
  • IO操作中的一个重要注意点

    JAVA是跨平台的语言,所以每个平台的文件地址的表示都不一样 例如:windows d:\src linux d:/src 所以 FileInputStream fi = new FileInputStream("d:/src") ; 应该改为 FileInputStream fi = new FileInputStream...

    闲话杂谈 2012 年 02 月 16 日
  • eclipse报错,Background Indexer Crash Recovery

    启动eclipse,报错Background Indexer Crash Recovery网上找了很多东西,什么删除jar包等等,解决不了问题干脆一下子把eclipse给删除掉了重新弄一个,搞定

    闲话杂谈 2012 年 06 月 04 日

评论列表(2条)

  • stupid 2012 年 02 月 21 日 上午 11:37

    没见过这么傻X的测试,你要用多线程并发才能测出区别阿!!

    • admin 2012 年 02 月 26 日 下午 4:26

      @stupid是呀,的确是傻X呀~~~
      之前根本不懂~嘿嘿~~