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

相关推荐

  • windows下部署JAVA项目

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

    闲话杂谈 2012 年 10 月 21 日
  • 在eclipse中查看android源码

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

    闲话杂谈 2012 年 02 月 01 日
  • HashMap与ConcurrentHashMap性能测试

    http://blog.csdn.net/liuzhengkang/article/details/2916829 http://blog.sina.com.cn/s/blog_605f5b4f0100qsio.html http://hi.baidu.com/fallen9/blog/item/690e963d56471209bba1670a.html http://www.xue5.com/…

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

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

    闲话杂谈 2013 年 08 月 19 日
  • 天猫淘宝,关于第三方淘宝客促销的那点破事

    第三方淘宝客促销,如zhe800 ,卷皮网等等的网站促销! 由于这些淘宝客的流量大,促销活动过后可以达到几千甚至过万的销量!   促销前,估计运营都衡量过得失,获得高销量的同时,有可能导致其他方面的负面影…

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

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

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

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

    闲话杂谈 2011 年 09 月 16 日
  • [转]分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里

    分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里。 由于公司要求读取项目下所有jsp文件,并读取开头的自定义的注释来生成类似javadoc的东西进行页面管理。 而读取jsp源…

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

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

    闲话杂谈 2012 年 05 月 09 日
  • 可恨,我被DNS挟持了?打开京东默认跳转到推广链接

    打开京东跳转到推广链接~ 我现在用的是电信网络,经过多方面测试,我怀疑是电信DNS挟持,其实很早之前就发现在浏览器上面输入网址: JD.COM,就会默认跳转到推广的链接,只不过一直没有跟它较劲,今天突发神经,坚…

    2016 年 07 月 01 日

评论列表(2条)

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

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

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

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