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

相关推荐

  • 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 日
  • 在eclipse中查看android源码

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

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

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

    闲话杂谈 2011 年 12 月 19 日
  • java中HashMap,LinkedHashMap,TreeMap,HashTable的区别

    java中HashMap,LinkedHashMap,TreeMap,HashTable的区别 java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMap Hashtable LinkedHashMap 和TreeMap Map主要用于存储健值对,根据键得到…

    闲话杂谈 2011 年 08 月 30 日
  • bo.android应用系列:文件创建

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

    2012 年 02 月 10 日
  • java 日期与毫秒相互转化

    因需要,所以写了个日期转化成毫秒,把毫秒转化成日期的类 import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; /** * 测试时间 * 把毫秒转换成 yyyy-MM-dd HH:mm:ss * @auth…

    闲话杂谈 2011 年 08 月 22 日
  • 多想一点,多做一点[转]

    1 先来看个例子: ‍‍。。。 2 再举个例子: 不知道男同学们有没有帮女同学在自动售货机买过饮料,买完以后顺手递给她,这一切都很自然。 那么你会不会在递给她之前,顺手帮她把瓶子盖拧开? 顺手把某个人的Hi发给对…

    闲话杂谈 2011 年 08 月 26 日
  • eclipse报错,Background Indexer Crash Recovery

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

    闲话杂谈 2012 年 06 月 04 日
  • synchronized 的4种用法

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

    闲话杂谈 2011 年 08 月 23 日
  • bo.android应用:电话拨号器

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

    2012 年 02 月 01 日

评论列表(2条)

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

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

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

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