HashTable HashMap TreeSet TreeMap性能测试,比较。
来源:互联网 发布:ai人工智能教程 编辑:程序博客网 时间:2024/05/29 11:26
很多人都知道有HASHMAP和HASHTABLE这两种类,但是不知道具体该用哪个,我也不是很清楚,写了个小程序测验:
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.TreeSet;
public class sa {
public static void main(String[] args) {
double pow=Math.pow(8,7);
time(pow);
}
public static void time(double pow){
double i=0;
Hashtable table=new Hashtable<Double,Double>();
HashMap hmap=new HashMap<Double,Double>();
TreeSet set=new TreeSet<Double>();
TreeMap tmap=new TreeMap<Double,Double>();
SimpleDateFormat sd = new SimpleDateFormat("ss:SSS");
String s1=sd.format(new Date());
System.out.println("正在存数。。。"+s1);
for(i=0;i<pow;i++){
table.put(i,i);
tmap.put(i,i);
hmap.put(i,i);
set.add(i);
}
String s2=sd.format(new Date());
System.out.println("存数完成。。。"+s2);
int vvv1=(int)Math.abs(Double.parseDouble(s2.substring(0,2))-Double.parseDouble(s1.substring(0,2)));//秒
int vvv2=(int)(Math.abs(Double.parseDouble(s2.substring(3))-Double.parseDouble(s1.substring(3))));//毫秒
System.out.println("用时"+vvv1+":"+vvv2+"\n");
//////////////////////////Hashmap
s1=sd.format(new Date());
System.out.println("正在取hashmap数。。。"+s1);
for(i=0;i<pow;i++){
hmap.get(i);
}
s2=sd.format(new Date());
System.out.println("取数完成。。。"+s2+" 查找次数:"+i);
vvv1=(int)(Double.parseDouble(s2.substring(0,2))-Double.parseDouble(s1.substring(0,2)));//秒
vvv2=(int)(Math.abs(Double.parseDouble(s2.substring(3))-Double.parseDouble(s1.substring(3))));//毫秒
System.out.println("用时"+vvv1+":"+vvv2+"\n");
//////////////////////////Treeset
s1=sd.format(new Date());
System.out.println("正在取treeset数。。。"+s1);
Iterator ss = set.iterator();
int i1=0;
while(ss.hasNext()){
i1++;
ss.next();
}
s2=sd.format(new Date());
System.out.println("取数完成。。。"+s2+" 查找次数:"+i1);
vvv1=(int)(Double.parseDouble(s2.substring(0,2))-Double.parseDouble(s1.substring(0,2)));//秒
vvv2=(int)(Math.abs(Double.parseDouble(s2.substring(3))-Double.parseDouble(s1.substring(3))));//毫秒
System.out.println("用时"+vvv1+":"+vvv2);
////////////////////////////Hashtable
s1=sd.format(new Date());
System.out.println("正在取hashtable数。。。"+s1);
for(i=0;i<pow;i++){
table.get(i);
}
s2=sd.format(new Date());
System.out.println("取数完成。。。"+s2+" 查找次数:"+i);
vvv1=(int)(Double.parseDouble(s2.substring(0,2))-Double.parseDouble(s1.substring(0,2)));//秒
vvv2=(int)(Math.abs(Double.parseDouble(s2.substring(3))-Double.parseDouble(s1.substring(3))));//毫秒
System.out.println("用时"+vvv1+":"+vvv2+"\n");
//////////////////////////Treemap
s1=sd.format(new Date());
System.out.println("正在取treemap数。。。"+s1);
for(i=0;i<pow;i++){
tmap.get(i);
}
s2=sd.format(new Date());
System.out.println("取数完成。。。"+s2+" 查找次数:"+i);
vvv1=(int)(Double.parseDouble(s2.substring(0,2))-Double.parseDouble(s1.substring(0,2)));//秒
vvv2=(int)(Math.abs(Double.parseDouble(s2.substring(3))-Double.parseDouble(s1.substring(3))));//毫秒
System.out.println("用时"+vvv1+":"+vvv2+"\n");
}
}
测试结果:
测试一:
正在存数。。。38:479
存数完成。。。46:800
用时8:321
正在取hashmap数。。。46:800
取数完成。。。46:901 查找次数:2097152.0
用时0:101
正在取treeset数。。。46:901
取数完成。。。46:951 查找次数:2097152
用时0:50
正在取hashtable数。。。46:951
取数完成。。。47:071 查找次数:2097152.0
用时1:880
正在取treemap数。。。47:071
取数完成。。。47:341 查找次数:2097152.0
用时0:270
测试二:
正在存数。。。14:340
存数完成。。。22:665
用时8:325
正在取hashmap数。。。22:665
取数完成。。。22:755 查找次数:2097152.0
用时0:90
正在取treeset数。。。22:755
取数完成。。。22:813 查找次数:2097152
用时0:58
正在取hashtable数。。。22:814
取数完成。。。22:937 查找次数:2097152.0
用时0:123
正在取treemap数。。。22:938
取数完成。。。24:727 查找次数:2097152.0
用时2:211
测试三:
正在存数。。。43:961
存数完成。。。55:626
用时12:335
正在取hashmap数。。。55:627
取数完成。。。55:719 查找次数:2097152.0
用时0:92
正在取treeset数。。。55:719
取数完成。。。55:762 查找次数:2097152
用时0:43
正在取hashtable数。。。55:763
取数完成。。。55:882 查找次数:2097152.0
用时0:119
正在取treemap数。。。55:882
取数完成。。。56:154 查找次数:2097152.0
用时1:728
总结下来HashMap性能还是优于HashTable的,TreeMap和TreeSet也放在一起测试了下,为的是看看树结构的和HASH结构的哪个更快,感觉差不多,也可能是时间太多了,大伙可以自己修改pow()方法中的参数,把时间调久点。
public static void main(String[] args) {
double pow=Math.pow(8,7);
time(pow);
}
- HashTable HashMap TreeSet TreeMap性能测试,比较。
- HashMap TreeMap Hashtable LinkedHashMap 比较
- HashMap、TreeMap、Hashtable、LinkedHashMap比较
- HashMap、Hashtable、LinkedHashMap、TreeMap比较
- java中的几种泛型类——HashSet、HashMap、TreeSet、TreeMap,遍历map,排序,HashTable比较
- TreeSet、TreeMap、HashSet、HashMap、HashTable、ArrayList、LinkedList、Vector集合的全部比较
- HashMap、Hashtable、LinkedHashMap和TreeMap 比较
- HashMap、Hashtable、LinkedHashMap和TreeMap 比较
- HashMap、LinkedHashMap、HashTable、TreeMap的比较
- HashMap、TreeMap、Hashtable、LinkedHashMap四者比较
- HashMap 、LinkedHashMap、HashTable、TreeMap的比较
- LinkedHashMap、HashMap、Hashtable、TreeMap的比较使用
- LinkedHashMap、HashMap、HashTable、TreeMap、ConcurrentHashMap的比较
- 深入Java集合源码学习系列:比较HashMap、Hashtable、TreeMap、ConcurrentHashMap、WeakHashMap性能
- HashSet, TreeSet, HashMap, TreeMap
- TreeMap、HashMap、ConcurrentSkipListMap之性能比较
- HashTable,HashMap和TreeMap
- HashTable HashMap TreeMap 区别
- Shell脚本调试小技巧
- java——泛型
- 【Leet Code】73. Set Matrix Zeroes---Medium
- UVA 11584 Partitioning by Palindromes
- 收藏下 jquery 选择器总结
- HashTable HashMap TreeSet TreeMap性能测试,比较。
- ios小数向上、下取整,计算结果向上、下取整
- 二值图像的腐蚀运算及其VC实现
- 读书笔记Cocos2d-x之场景、层
- Linux-网桥原理分析
- Ubuntu登陆密码忘记后找回方式
- startActivityForResult使用步骤分解
- 从零开始机器学习实战2-Matplotlib绘图
- 《ARM与Linux些许问题》第四章:ARM平台系统调用原理分析