HashMap与ConcurrentHashMap性能对比

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 日

相关推荐

  • synchronized 的4种用法

    概述: synchronized 关键字,代表这个方法加锁,相当于不管哪一个线程A每次运行到这个方法时,都要检查有没有其它正在用这个方法的线程B(或者C D等),有的话要等正在使用这个方法的线程B(或者C D)运行完这个方...

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

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

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

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

    闲话杂谈 2013 年 08 月 19 日
  • 想转SAP FICO顾问的必看 (转)

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

    闲话杂谈 2012 年 02 月 26 日
  • Eclipse快捷键大全(转载)

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

    闲话杂谈 2011 年 12 月 19 日
  • 判断商标相同或近似的标准

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

    闲话杂谈 2014 年 05 月 23 日
  • eclipse报错,Background Indexer Crash Recovery

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

    闲话杂谈 2012 年 06 月 04 日
  • 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 日
  • 哪个银行信用卡最好用?哪个银行信用卡最好

    四大国有商业银行: 建设银行: 优点: 1、免首年年费,刷三次免次年年费; 2、短信服务免费; 3、取现手续费仅0.5%; 4、挂失手续费50元; 5、有直接针对有车族信用卡,只要有1.4排量以上的车均能轻松办卡。 缺...

    闲话杂谈 2011 年 10 月 09 日
  • 日语,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 日

评论列表(2条)

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

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

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

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