HashTable HashMap TreeSet TreeMap性能测试,比较。

来源:互联网 发布:ai人工智能教程 编辑:程序博客网 时间:2024/05/29 11:26

很多人都知道有HASHMAP和HASHTABLE这两种类,但是不知道具体该用哪个,我也不是很清楚,写了个小程序测验:


import java.text.SimpleDateFormat;
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);
}



1 0
原创粉丝点击