日期:2008-9-10

测试平台:

CPU:Intel Pentium(R) 4 CPU 3.06G

内存:4G

操作系统:window server 2003

一、HashMapConcurrentHashMap简单put操作的时间对比

 

1HashMap测试

A、程序代码:

package test0908;

import java.util.Map;

import java.util.HashMap;

public class HashmapTest {

public static void main(String []args){

Map<Integer,Integer> hashmap = new HashMap<Integer,Integer>();

int tt=13;

而循环100万

Hashmap.put(i,”aaa”),

用时time = 2563ms

long begin1 = System.currentTimeMillis();

for(int i=0; i<1000000; i++){

tt = Math.abs(tt*(tt-i)-119);

hashmap.put(tt, tt);

//System.out.println(hashmap.get(tt));

}

System.out.println("time="+(System.currentTimeMillis() - begin1)+"ms.");

}

}

B、测试结果截图(循环100万次):

当put操作循环10万次时,得到time = 344ms,

循环50万次时,得到time = 1657ms,

循环100万次时,得到time =4094ms。

2ConcurrentHashMap测试

A、程序代码:

package test0908;

import java.util.concurrent.ConcurrentHashMap;

public class conHashmapTest{

public static void main(String []args){

ConcurrentHashMap<Integer,Integer> chashmap = newConcurrentHashMap<Integer,Integer>();

int tt=13;

long begin1 = System.currentTimeMillis();

for(int i=0; i<1000000; i++){

tt = Math.abs(tt*(tt-i)-119);

chashmap.put(tt, tt);

//System.out.println(hashmap.get(tt));

}

System.out.println("time="+(System.currentTimeMillis() - begin1)+"ms.");

}

}

B、测试结果截图(循环100万次):

当put操作循环10万次时,得到time =281ms,

循环50万次时,得到time = 1376ms,

循环100万次时,得到time =3625ms,

二、HashMapConcurrentHashMap  put操作的最多个数对比(即内存溢出)

1、 HashMap测试

测试结果截图:

运行程序,内存初值为:846M,内存峰值为:931M,put计数=1,030,604

 

2、 ConcurrentHashMap  测试

测试结果截图:

运行程序,内存初值为:847M,内存峰值为:931M,put计数=1,030,238

三、HashMapConcurrentHashMap  多线程操作的测试

 

1、  HashMap测试结果截图:(10个put线程,8个get线程)

平均每秒的get次数/总get次数

平均每秒的put次数/总Put次数

 

2、  ConcurrentHashMap  测试结果截图 :(10个put线程,8个get线程)

 

3、  以上均设置睡眠1ms时, 平均每个线程达到510多;

每秒平均put的次数随线程的个数增加而增加,

4、注:当put线程数量为100get线程数量为90时,HashMap就开始出现性能下降的情形,CPU使用率达到45%左右,且putget的个数要明显少于ConcurrentHashMap的个数;

而使用ConcurrentHashMap时,则线程很稳定,CPU使用率不超过12%时。

测试结果截图:

与concurrenthashmap相比,Put,get线程达到100个条件下,hashmap要少5500左右

AHashMap测试图:

 

B、 ConcurrentHashMap测试图:

 

5、经反复测试发现,只要创建的putget的线程总数达到180个以上时,HashMap的性能就开始下降。而当创建的putget的线程总数达到256个以上时,ConcurrentHsahMap的性能也开始下降。

性能下降:CPU的使用率开始增加,平均每秒put和get的个数开始下降,即出现若线程再增加,而put和get反而减少。

发一篇原贴文章真不容易啊,尤其是还这么多图片的。嘻嘻!搞了我半个多钟头。这个测试报告是写给项目经理看的,但是很多同志说看不懂这个报告里面有什么../? 所以发上来希望各位高手狠狠的给点意见,小弟在此谢了!

最后贴上第三个测试中concurrenthashmap的源程序:

package test0908;

import java.util.concurrent.ConcurrentHashMap;

public class CHashmapTest {

public static void main(String []args){

try{

count c = new count();

ConcurrentHashMap<Integer,Integer> chm = new ConcurrentHashMap<Integer,Integer>();

for(int i=0; i<50; i++){

new putCHashmapThread(chm,c).start(); //put操作

}

for(int i =0 ; i<45; i++){

new getCHashmapThread(chm,c).start();    //get操作

}

ProbeThread pt = new ProbeThread(c);    //监听线程

pt.start();

}catch(Exception e){

System.out.println(e.getMessage());

}

}

}

class putCHashmapThread extends Thread{     //put操作线程

private ConcurrentHashMap<Integer,Integer> chm = null;

private count c = null;

public putCHashmapThread(ConcurrentHashMap<Integer,Integer> chm,count c){

this.chm = chm;

this.c = c;

}

public void run(){

int tt = 13;

int i = 1;

try{

while(true){

tt = Math.abs(tt*(tt-i)-119);

chm.put(tt, tt);

c.addcount1();    //put操作计数

i++;

Thread.sleep(1);

//System.out.println(i);

}

}catch(Exception e){

System.out.println(e.getMessage());

}

}

}

class getCHashmapThread extends Thread{      //get操作线程

private ConcurrentHashMap<Integer,Integer> chm = null;

private count c = null;

public getCHashmapThread(ConcurrentHashMap<Integer,Integer> chm,count c){

this.chm = chm;

this.c = c;

}

public void run(){

int tt = 13;

int i = 1;

try{

while(true){

tt = Math.abs(tt*(tt-i)-119);

chm.get(tt);

c.addcount2();  //get操作计数

i++;

Thread.sleep(1);

//System.out.println(i);

}

}catch(Exception e){

System.out.println(e.getMessage());

}

}

}

class count{   //计数器

private static int count1 = 1, count2 = 1;

public int getcount1() {

return count1;

}

public int getcount2(){

return count2;

}

public void addcount1(){

count1++;

}

public void addcount2(){

count2++;

}

}

class ProbeThread extends Thread {     //监听线程

private boolean run = true;

count cc;

public ProbeThread(count cc) {

this.cc = cc;

}

@SuppressWarnings("static-access")

public void run() {

int c1=0, c2=0;

int cc1 = 0, cc2 = 0;

while(this.run) {

c2 = cc.getcount1();

cc2 =cc.getcount2();

System.out.println("put:"+"["+(c2-c1)/2+"/"+c2+"]"+"  get:"+"["+(cc2-cc1)/2+"/"+cc2+"]");

c1 = c2;

cc1 = cc2;

try {

Thread.sleep(1000*2-1);

} catch(Exception ex) {

System.out.println("Error[ProbeThread.run]:"+ex.getMessage());

}

}

}

}

HashMap与ConcurrentHashMap的测试报告的更多相关文章

  1. [Java集合] 彻底搞懂HashMap,HashTable,ConcurrentHashMap之关联.

    注: 今天看到的一篇讲hashMap,hashTable,concurrentHashMap很透彻的一篇文章, 感谢原作者的分享. 原文地址: http://blog.csdn.net/zhanger ...

  2. HashMap和ConcurrentHashMap流程图

    本文表达HashMap和ConcurrentHashMap中的put()方法的执行流程图,基于JDK1.8的源码执行过程. HashMap的put()方法: ConcurrentHashMap的put ...

  3. 轻松理解 Java HashMap 和 ConcurrentHashMap

    前言 Map 这样的 Key Value 在软件开发中是非常经典的结构,常用于在内存中存放数据. 本篇主要想讨论 ConcurrentHashMap 这样一个并发容器,在正式开始之前我觉得有必要谈谈 ...

  4. Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析

    Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析 今天发一篇”水文”,可能很多读者都会表示不理解,不过我想把它作为并发序列文章中不可缺少的一块来介绍.本来以为花不了 ...

  5. Java中关于Map的使用(HashMap、ConcurrentHashMap)

    在日常开发中Map可能是Java集合框架中最常用的一个类了,当我们常规使用HashMap时可能会经常看到以下这种代码: Map<Integer, String> hashMap = new ...

  6. Java7/8 中 HashMap 和 ConcurrentHashMap的对比和分析

    大家可能平时用HashMap比较多,相对于ConcurrentHashMap 来说并不是很熟悉.ConcurrentHashMap 是 JDK 1.5 添加的新集合,用来保证线程安全性,提升 Map ...

  7. 高并发第九弹:逃不掉的Map --> HashMap,TreeMap,ConcurrentHashMap

    平时大家都会经常使用到 Map,面试的时候又经常会遇到问Map的,其中主要就是 ConcurrentHashMap,在说ConcurrentHashMap.我们还是先看一下, 其他两个基础的 Map ...

  8. 深入理解HashMap和concurrentHashMap

    原文链接:https://segmentfault.com/a/1190000015726870 前言 Map 这样的 Key Value 在软件开发中是非常经典的结构,常用于在内存中存放数据. 本篇 ...

  9. 沉淀再出发:java中的HashMap、ConcurrentHashMap和Hashtable的认识

    沉淀再出发:java中的HashMap.ConcurrentHashMap和Hashtable的认识 一.前言 很多知识在学习或者使用了之后总是会忘记的,但是如果把这些只是背后的原理理解了,并且记忆下 ...

随机推荐

  1. Android:Field can be converted to a local varible.

    背景 使用 Android Studio 开发 Android 有一段时间了,偶尔会碰到 AS 在一些私有变量上有黄色高亮提示Field can be converted to a local var ...

  2. Andriod Studio科普篇——4.关于编译的常见问题

    1.android支持库未安装 编译不过,提示如下: Could not find any version that matches com.android.support:appcompat-v7: ...

  3. studio安装插件

    Android Studio安装插件的方式其实和Eclipse大同小异.废话不多说,直接上图: 区域1:你当前已经安装了的插件 区域2:在线安装 区域3:从硬盘安装,即针对你已经下载好了的插件,可通过 ...

  4. (NO.00004)iOS实现打砖块游戏(四):砖块类的实现

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 用Xcode打开之前SpriteBuilder创建的项目,我们现 ...

  5. Android学习之Animation(一)

    3.0以前,android支持两种动画模式,Tween Animation,Frame Animation,在android3.0中又引入了一个新的动画系统:Property Animation,这三 ...

  6. ROS_Kinetic_15 ROS使用Qt

    ROS_Kinetic_15 ROS使用Qt 在网页http://www.qt.io/download-open-source/#section-2 下载并安装Qt ~/下载$ chmod +x qt ...

  7. Ext JS 6应用程序Build后出现“c is not a constructor return new c(a[0])”的处理

    概述 在对Ext JS 6的应用程序打包后,时不时会出现以下错误: 由于是压缩后出现的错误,要进行调试也无从下手,因而这个错误会令新手手足无措,不知道是怎么回事. 错误原因 造成该错误的主要原因是要创 ...

  8. JAVA之旅(十五)——多线程的生产者和消费者,停止线程,守护线程,线程的优先级,setPriority设置优先级,yield临时停止

    JAVA之旅(十五)--多线程的生产者和消费者,停止线程,守护线程,线程的优先级,setPriority设置优先级,yield临时停止 我们接着多线程讲 一.生产者和消费者 什么是生产者和消费者?我们 ...

  9. Java接口interface

    Java接口interface 1.多个无关的类可以实现同一个接口. 2.一个类可以实现多个无关的接口. 3.与继承关系类似,接口与实现类之间存在多态性. 接口(interface)是抽象方法和常量值 ...

  10. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...