说明:我所涉及到的试验代码,均是针对于1百万条记录,我的硬件配置如下:CPUP4 1.5G,256M DDR的内存。
如果要试验其代码,要采用这种方式运行:java -Xms128M -Xmx250M TestMap
1,对内存容量的要求
Map对象高于List。因为Map除了Value外还需要一个Object的Key,从而增大了Map的容量。试验代码如下:
import java.util.*;
public class TestMap {
public static void main(String[] args) {
System.out.println("begin>>>>>>>>>>>>>>");
long sm = Runtime.getRuntime().totalMemory();
System.out.println("Begin Memory :" + sm);
Map m = new Hashtable();
//LinkedList tmpList = new LinkedList();
for (int i=0;i<1000000;i++) {
m.put(""+i,"i=" + i);
//Integer integer = new Integer(i);
//tmpList.add(integer);
}
long em = Runtime.getRuntime().totalMemory();
System.out.println("End Memory :" + em);
System.out.println("End-Start Memory :" + (em - sm));
//walkList(tmpList);
System.out.println("<<<<<<<<<<<<<<<<end");
//find(m,"985757");
walkMap(m);
//List tmpList = new LinkedList();
//tmpList.addAll(m.values());
}
}
其对比结果如下:1百万条记录Map大概需要70M额外空间,而List不需要,其额外空间为0
2.遍历比较
我在试验中,发现我们经常采用遍历List的方式是一个错误的遍历方式,其性能其差无比。我们经常遍历List代码实例如下:
for(int i = 0;i<list.size();i++) {
Object obj = list.get(i);
}
如果List的遍历方式换一种方式,其性能和Map差不多,我们将上面的代码进行扩充,从而来比较Map和List的遍历速度:
import java.util.*;
/**
* @author whxu
* @version 1.0.0 2003-4-22 AM
*/
public class TestMap {
public static void main(String[] args) {
System.out.println("begin>>>>>>>>>>>>>>");
long sm = Runtime.getRuntime().totalMemory();
System.out.println("Begin Memory :" + sm);
Map m = new Hashtable();
//LinkedList tmpList = new LinkedList();
for (int i=0;i<1000000;i++) {
m.put(""+i,"i=" + i);
//Integer integer = new Integer(i);
//tmpList.add(integer);
}
long em = Runtime.getRuntime().totalMemory();
System.out.println("End Memory :" + em);
System.out.println("End-Start Memory :" + (em - sm));
//walkList(tmpList);
System.out.println("<<<<<<<<<<<<<<<<end");
//find(m,"985757");
walkMap(m);
//List tmpList = new LinkedList();
//tmpList.addAll(m.values());
}
public static void find(Map m,String key) {
long st = System.currentTimeMillis();
Object obj = m.get(key);
System.out.println("Object:" + obj);
long et = System.currentTimeMillis();
System.out.println("Start Time:" + st);
System.out.println("End Time:" + et);
System.out.println("Use Time:" + (et - st));
}
public static void walkMap(Map m) {
long st = System.currentTimeMillis();
for(Iterator it = m.values().iterator();it.hasNext();) {
Object obj = it.next();
}
long et = System.currentTimeMillis();
System.out.println("WalkMap Start Time:" + st);
System.out.println("WalkMap End Time:" + et);
System.out.println("WalkMap Use Time:" + (et - st));
}
public static void walkList(LinkedList l) {
long st = System.currentTimeMillis();
Object obj = null;
//System.out.println(l.get(3434));
/*这种遍历方式的性能其差无比,建议不要使用
for(int i=0;i<l.size();i++) {
obj = l.get(i);
}
*/
for(Iterator it = l.iterator();it.hasNext();) {
obj = it.next();
}
long et = System.currentTimeMillis();
System.out.println("WalkList Start Time:" + st);
System.out.println("WalkList End Time:" + et);
System.out.println("WalkList Use Time:" + (et - st));
}
}
3.随机查找。如果Map知道了Key,List知道了index,其性能在一百万条数据中无多大差别。其实例代码如下:
import java.util.*;
/**
* @author whxu
* @version 1.0.0 2003-4-22 AM
*/
public class TestMap
{
public static void main(String[] args)
{
System.out.println("begin>>>>>>>>>>>>>>");
long sm = Runtime.getRuntime().totalMemory();
System.out.println("Begin Memory :" + sm);
Map m = new Hashtable();
LinkedList tmpList = new LinkedList();
for (int i=0;i<1000000;i++) {
//m.put(""+i,"i=" + i);
tmpList.add("i=" + i);
}
long em = Runtime.getRuntime().totalMemory();
System.out.println("End Memory :" + em);
System.out.println("End-Start Memory :" + (em - sm));
//walkList(tmpList);
System.out.println("<<<<<<<<<<<<<<<<end");
//find(m,"985757");
find(tmpList,1000000-1);
//walkMap(m);
//List tmpList = new LinkedList();
//tmpList.addAll(m.values());
}
public static void find(Map m,String key) {
long st = System.currentTimeMillis();
Object obj = m.get(key);
System.out.println("Find Map Object:" + obj);
long et = System.currentTimeMillis();
System.out.println("Find Map Start Time:" + st);
System.out.println("Find Map End Time:" + et);
System.out.println("Find Map Use Time:" + (et - st));
}
public static void find(List l,int index) {
long st = System.currentTimeMillis();
Object obj = l.get(index);
System.out.println("Find List Object:" + obj);
long et = System.currentTimeMillis();
System.out.println("Find List Start Time:" + st);
System.out.println("Find List End Time:" + et);
System.out.println("Find List Use Time:" + (et - st));
}
public static void walkMap(Map m) {
long st = System.currentTimeMillis();
for(Iterator it = m.values().iterator();it.hasNext();) {
Object obj = it.next();
}
long et = System.currentTimeMillis();
System.out.println("WalkMap Start Time:" + st);
System.out.println("WalkMap End Time:" + et);
System.out.println("WalkMap Use Time:" + (et - st));
}
public static void walkList(LinkedList l) {
long st = System.currentTimeMillis();
Object obj = null;
//System.out.println(l.get(3434));
/*for(int i=0;i<l.size();i++) {
obj = l.get(i);
}
*/
for(Iterator it = l.iterator();it.hasNext();) {
obj = it.next();
}
long et = System.currentTimeMillis();
System.out.println("WalkList Start Time:" + st);
System.out.println("WalkList End Time:" + et);
System.out.println("WalkList Use Time:" + (et - st));
}
}
posted on 2007-06-02 11:35
分享爱的空间 阅读(1388)
评论(0) 编辑 收藏