HashMap与ConcurrentHashMap的测试报告
日期:2008-9-10
测试平台:
CPU:Intel Pentium(R) 4 CPU 3.06G
内存:4G
操作系统:window server 2003
一、HashMap与ConcurrentHashMap简单put操作的时间对比
1、HashMap测试
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
for(int i=0; i<1000000; i++){
tt = Math.abs(tt*(tt-i)-119);
//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。
2、ConcurrentHashMap测试
A、程序代码:
package test0908;
import java.util.concurrent.ConcurrentHashMap;
public class conHashmapTest{
public static void main(String []args){
ConcurrentHashMap<Integer,Integer> chashmap = new ConcurrentHashMap<Integer,Integer>();
int tt=13;
long begin1 = System.currentTimeMillis();
for(int i=0; i<1000000; i++){
tt = Math.abs(tt*(tt-i)-119);
//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,
二、HashMap与ConcurrentHashMap put操作的最多个数对比(即内存溢出)
1、 HashMap测试
测试结果截图:
运行程序,内存初值为:846M,内存峰值为:931M,put计数=1,030,604
2、 ConcurrentHashMap 测试
测试结果截图:
运行程序,内存初值为:847M,内存峰值为:931M,put计数=1,030,238
三、HashMap与ConcurrentHashMap 多线程操作的测试
1、 HashMap测试结果截图:(10个put线程,8个get线程)
平均每秒的get次数/总get次数
平均每秒的put次数/总Put次数
2、 ConcurrentHashMap 测试结果截图 :(10个put线程,8个get线程)
3、 以上均设置睡眠1ms时, 平均每个线程达到510多;
每秒平均put的次数随线程的个数增加而增加,
4、注:当put线程数量为100,get线程数量为90时,HashMap就开始出现性能下降的情形,CPU使用率达到45%左右,且put、get的个数要明显少于ConcurrentHashMap的个数;
而使用ConcurrentHashMap时,则线程很稳定,CPU使用率不超过12%时。
测试结果截图:
与concurrenthashmap相比,Put,get线程达到100个条件下,hashmap要少5500左右
B、 ConcurrentHashMap测试图:
5、经反复测试发现,只要创建的put和get的线程总数达到180个以上时,HashMap的性能就开始下降。而当创建的put