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 日 上午 9:47
下一篇 2011 年 08 月 30 日 下午 5:54

相关推荐

  • Eclipse快捷键大全(转载)

    Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+↓ 当前行和下面一行交互位置(特别实用,可以省去先剪切,…

    闲话杂谈 2011 年 12 月 19 日
  • 在eclipse中查看android源码

    网上找到一个打包好的源码地址 // 可以直接下载打包好的源码(1.5/16./2.0/2.1/2.2) 地址:http://rgruet.free.fr/public/     例如我在2.2的基础上开发,那么把下载到的2.2源码放到 SDK的这个目录下即…

    闲话杂谈 2012 年 02 月 01 日
  • windows下部署JAVA项目

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

    闲话杂谈 2012 年 10 月 21 日
  • 杂谈,老钟这些日子都在做啥子了?为什么一直都没有更新

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

    闲话杂谈 2015 年 09 月 16 日
  • bo.android应用系列:文件创建

    目前功能:创建一个文件,并写入内容 ======================== 截图:               ======================== 部分代码: /** * <p> * 业务层<br/> * 2012-02-0…

    2012 年 02 月 10 日
  • 解决servlet返回jsp中文乱码

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

    闲话杂谈 2012 年 02 月 02 日
  • svn在eclipse中报错:Attempted to lock an already-locked dir

    报错信息如下:Problems encountered while deleting resources.  org.tigris.subversion.javahl.ClientException: Attempted to lock an  already-locked …

    闲话杂谈 2012 年 10 月 22 日
  • 判断商标相同或近似的标准

    上如何界定相同或类似商品、服务之后,有必要对相同商标、近似商标的概念再进行界定,之后才有利于认定商标是否相同或近似。 1.相同商标 相同商标是指构成商标的要素完全相同,商标在整体上不存在差别或者只存在细…

    闲话杂谈 2014 年 05 月 23 日
  • java 日期与毫秒转换,计算指定日期后N天

    最近写一些工具类中,遇到一些日期转换的问题,特意把方法分享,其还有很大的改进空间 /** * 把毫秒转化为日期<br/> * 转化后格式为 yyyy-MM-dd HH:mm:ss * * @param ms * @return 日期字符串 */ public stat…

    闲话杂谈 2011 年 09 月 23 日
  • 匹配邮箱的工具类

    参考做了一个匹配邮箱的工具类,方便以后使用: package com.util; import java.util.List; import java.util.Map; /** * 通过正则匹配全文邮箱 * * @author Stany 2011-08-05 */ public class EmailUtil { /** * …

    闲话杂谈 2011 年 08 月 05 日

评论列表(2条)

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

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

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

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