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

相关推荐

  • 判断商标相同或近似的标准

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

    闲话杂谈 2014 年 05 月 23 日
  • java编程中提升性能的一些方法【转】

    最近的机器内存又爆满了,出了新增机器内存外,还应该好好review一下我们的代码,有很多代码编写过于随意化,这些不好的习惯或对程序语言的不了解是应该好好打压打压了。 下面是参考网络资源和总结一些在java编程中…

    闲话杂谈 2011 年 08 月 16 日
  • Android Please execute ‘adb uninstall com.ims.poc.ui’ in a shell.

    [2010-03-23 11:57:49 - Poc]Re-installation failed due to different application signatures. [2010-03-23 11:57:49 - Poc]You must perform a full uninstall of the application. WARNING: This will remove t…

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

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

    闲话杂谈 2012 年 02 月 02 日
  • 5.1节后杂谈一篇,关于新驾照,扣12分的行为

    劳动节,休息了几天,本来有非常多事情要做的,但是因为有更加重要的事情,所以假期必须回家去办事! 这世道,汽车已经非常普遍了,所以无论如何,都尽量有个驾照吧!之前大学时代一直没有去考,后来工作本想在广州…

    2014 年 05 月 04 日
  • eclipse报错,Background Indexer Crash Recovery

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

    闲话杂谈 2012 年 06 月 04 日
  • 记录android的学习开发过程

    全面记录android的学习开发过程!!

    闲话杂谈 2011 年 06 月 28 日
  • 天猫发票问题,注意事项

    天猫发票这个基本的规则真的大家要熟读 往往犯错导致犯规的就是这个问题,下面和大家分享一下规则 开发票不收费 (不收税点不加钱)可以开普通发票,也可以开增值税发票买家付了多少钱就开多少钱,…

    闲话杂谈 2014 年 02 月 14 日
  • IO操作中的一个重要注意点

    JAVA是跨平台的语言,所以每个平台的文件地址的表示都不一样 例如:windows d:\src linux d:/src 所以 FileInputStream fi = new FileInputStream("d:/src") ; 应该改为 FileInputStream fi = new FileInputStream("…

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

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

    2014 年 04 月 14 日

评论列表(2条)

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

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

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

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