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

相关推荐

  • iphone 验机

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

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

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

    闲话杂谈 2013 年 08 月 19 日
  • 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 日
  • 项目维护时,使用jacob出现的一些问题

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

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

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

    闲话杂谈 2011 年 08 月 05 日
  • java.lang.IncompatibleClassChangeError: Found interface com.mysql.jdbc.Statement, but class was expected

    Exception in thread "main" java.lang.IncompatibleClassChangeError: Found interface com.mysql.jdbc.Statement, but class was expected 昨天升级程序,在启动一个java类进行测试的时候出现这个问题,最后发现…

    闲话杂谈 2012 年 02 月 29 日
  • bo.android应用:电话拨号器

    看了传智播客的视频 初学,老鸟别笑 通过两个模拟器,可是实现拨号、通话 ======================================================= 先看效果图:   ======================================================= 部…

    2012 年 02 月 01 日
  • 买家更换地址,骗局?卖家要注意了~

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

    闲话杂谈 2013 年 08 月 19 日
  • Eclipse快捷键大全(转载)

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

    闲话杂谈 2011 年 12 月 19 日
  • Access restriction: The type Reflection is not accessible due to restrictio

        解决方法: Windows -> Preferences -> Java -> Compiler -> Errors/Warnings -> Deprecated and trstricted API -> Forbidden reference (access rules): -> change to warning

    闲话杂谈 2011 年 12 月 02 日

评论列表(2条)

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

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

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

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