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 日

相关推荐

  • 杂谈,老钟这些日子都在做啥子了?为什么一直都没有更新

    很长一段时间没有更新博客了,所以呀,坚持是一件非常艰难的事情。 对于能坚持一直做好一件事情的人,想不成功都难呀,表示敬仰呀! 虽然我博客没有更新,但是我的事业还是依然坚持着,平时工作的时间太长了,都...

    闲话杂谈 2015 年 09 月 16 日
  • 2014-12-04 完成了人生一件大事,婚礼

    今天29号了,在老家举办婚礼,因为事业的缘故,还没有好好地和老婆度蜜月,就赶回到公司工作! 老婆,我心感愧疚,往后好好地去爱你,用一生去守护你,同样也非常感谢一直以来你对我工作的支持!   老婆,能...

    2014 年 12 月 29 日
  • 老钟非常LOVE的一些电影

    挚爱、视觉和听觉都超级一流的电影: 钢铁侠 Iron Man(2008.04.30) 无敌浩克 The Incredible Hulk(2008.08.20) 钢铁侠2 Iron Man 2(2010.04.25) 雷神托尔 Thor(2011.05.02) 美国队长 Captain America: Th...

    2014 年 04 月 22 日
  • DNS挟持后续更新,问题解决了

    解决DNS挟持的办法就是投诉,不断投诉,上工信部投诉 上次提到,我这边的电信网络被挟持,具体原因就不多说了 可恨,我被DNS挟持了?打开京东默认跳转到推广链接 经过多方面的投诉,现在问题终于解决,很多电信工...

    闲话杂谈 2016 年 09 月 09 日
  • 想转SAP FICO顾问的必看 (转)

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

    闲话杂谈 2012 年 02 月 26 日
  • 买家更换地址,骗局?卖家要注意了~

     近期出现了因卖家的粗心,泄露了买家的个人信息导致买家被骗的情况。小淘提醒各位卖家:买家是卖家的上帝,保护买家的信息是卖家的基本职责。不轻易泄露买家的收货地址,如确有需要请再三确认对方身份。PS...

    闲话杂谈 2013 年 08 月 19 日
  • 项目维护时,使用jacob出现的一些问题

    以下是经过搜索,总结网友的一些总结,目前我遇到的问题还没有解决~~ 等重启服务器再试试~   1、如果出现下面的错误 com.jacob.com.ComFailException: A COM exception has been encountered: At Invoke of:...

    闲话杂谈 2011 年 09 月 16 日
  • Java 中 finally 语句块的深度辨析【转】

    可不能小看这个简单的 finally,看似简单的问题背后,却隐藏了无数的玄机。接下来我就带您一步一步的揭开这个 finally 的神秘面纱。 问题分析 首先来问大家一个问题:finally 语句块一定会执行吗? 很多人都认为 ...

    闲话杂谈 2011 年 08 月 16 日
  • Eclipse启动失败,JVM terminated.Exit code=1

    Eclipse启动失败,JVM terminated.Exit code=1解决办法:把eclipse文件夹下的eclipse.ini先copy到别的地方(备份一下),然后删除之,启动一下eclipse

    闲话杂谈 2012 年 05 月 09 日
  • 解决servlet返回jsp中文乱码

    servlet返回中文到jsp页面,出现乱码 解决方式: 声明编码在先,out在后 response.setContentType("text/html;charset=gbk"); PrintWriter out = response.getWriter();   转载请注明出处: http://www.laoz...

    闲话杂谈 2012 年 02 月 02 日

评论列表(2条)

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

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

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

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