奔跑的肥猪

导航

如何有效遍历集合中的对象

今天看了看practical java 中不要使用enumeration 或者iteration来便利对象,自己亲自试了一下,发现

如果如果针对小的数据集,效率问题不是很明显,对于大的数据集还有有一些差距的。由测试结果可以看出虽然loop

类比其他遍历方法少用一次查询方法,但是对于小的数据集,其结果比其他的遍历方法还要慢,但数据集越大loop的

优势越明显。

1. 对应的log类

package com.tn.app.log;

public class Log {
	private long startmemory;
	private long starttime;
	private static Log log;

	private Log() {
		super();
	}

	public static Log getInstance() {
		if (log == null)
			log = new Log();
		return log;

	}

	private void setStartMemory() {
		Runtime runtime = Runtime.getRuntime();
		runtime.gc();
		startmemory = runtime.totalMemory() - runtime.freeMemory();
	}

	private void SetStartTime() {
		starttime = System.currentTimeMillis();
	}

	public void start() {
		SetStartTime();
		setStartMemory();
	}

	public void end() {
		setEndMemory();
		SetEndTime();
	}

	private void setEndMemory() {
		Runtime runtime = Runtime.getRuntime();
		runtime.gc();
		long endMemory = runtime.totalMemory() - runtime.freeMemory();
		System.out.println("the consumed memory is"
				+ Math.abs((endMemory - startmemory)));
	}

	private void SetEndTime() {
		long endTime = System.currentTimeMillis();
		System.out.println("the consumed time is" + (endTime - starttime));
		System.out.println("===================================");
	}
}

  

2. 对用的反射类

package com.tn.refector;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.tn.app.log.Log;
import com.tn.list.Total;

public class ListRefector implements InvocationHandler {

	private Total total;
	private Log log;

	public ListRefector(Total targetCreator) {
		this.total = targetCreator;
	}

	public Object invoke(Object proxy, Method method, Object[] args)
			throws Throwable {
		log = Log.getInstance();
		log.start();
		Object result = method.invoke(this.total, args);
		log.end();
		return result;
	}

	@SuppressWarnings("unchecked")
	public static Object getListCreator(Total target) {
		System.out.println("class name is " + target.toString());
		Class targetClass = target.getClass();
		ClassLoader loader = targetClass.getClassLoader();
		Class[] interfaces = targetClass.getInterfaces();
		ListRefector handler = new ListRefector(target);
		return Proxy.newProxyInstance(loader, interfaces, handler);
	}

}

 

3.实现类

package com.tn.list;

import java.util.Vector;

public interface Total {
	int getTotal(Vector<Integer> vec);
}

  

package com.tn.list;

import java.util.Vector;

public class Loop implements Total {

	public int getTotal(Vector<Integer> vec) {
		int total = 0;
		int size = vec.size();
		for (int vecIndex = 0; vecIndex < size; vecIndex++) {
			total += vec.get(vecIndex).intValue();
		}
		return total;
	}

}

  

package com.tn.list;

import java.util.ListIterator;
import java.util.Vector;

public class ListIter implements Total {

	public int getTotal(Vector<Integer> vec) {
		int total = 0;
		ListIterator<Integer> listInte = vec.listIterator();
		while (listInte.hasNext())
			total += ((Integer) (listInte.next())).intValue();
		return total;
	}

}

  

package com.tn.list;

import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;

public class Iter implements Total {

	public int getTotal(Vector<Integer> vec) {
		int total = 0;
		Iterator<Integer> itera = vec.iterator();
		while (itera.hasNext())
			total += ((Integer) (itera.next())).intValue();
		return total;
	}

}

  

package com.tn.list;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;

public class Enume implements Total {

	public int getTotal(Vector<Integer> vec) {
		int total = 0;
		Enumeration<Integer> enume = vec.elements();
		while (enume.hasMoreElements())
			total += ((Integer) (enume.nextElement())).intValue();
		return total;
	}

}

  

4. 测试类

package com.tn.refector;

import java.util.Vector;

import com.tn.list.Enume;
import com.tn.list.Iter;
import com.tn.list.ListIter;
import com.tn.list.Loop;
import com.tn.list.Total;

public class Dynamic {
	private static Total target, proxy;

	public static void main(String[] args) {
		Vector<Integer> vec = InitVec(2000000);
		getTotalByEnum(vec);
		getTotalByIter(vec);
		getTotalByListIter(vec);
		getTotalByLoop(vec);
	}

	private static void getTotalByLoop(Vector<Integer> vec) {
		target = new Loop();
		proxy = (Total) ListRefector.getListCreator(target);
		proxy.getTotal(vec);
	}

	private static void getTotalByListIter(Vector<Integer> vec) {
		target = new ListIter();
		proxy = (Total) ListRefector.getListCreator(target);
		proxy.getTotal(vec);
	}

	private static void getTotalByIter(Vector<Integer> vec) {
		target = new Iter();
		proxy = (Total) ListRefector.getListCreator(target);
		proxy.getTotal(vec);
	}

	private static void getTotalByEnum(Vector<Integer> vec) {
		target = new Enume();
		proxy = (Total) ListRefector.getListCreator(target);
		proxy.getTotal(vec);
	}

	private static Vector<Integer> InitVec(int size) {
		Vector<Integer> vec = new Vector<Integer>();
		for (int startIndex = 0; startIndex < size; startIndex++) {
			vec.add(startIndex);
		}
		return vec;
	}
}

  

5. 结果如下:

当size =100000

class name is com.tn.list.Enume@757aef

the consumed memory is56

the consumed time is15

===================================

class name is com.tn.list.Iter@61de33

the consumed memory is16

the consumed time is31

===================================

class name is com.tn.list.ListIter@ca0b6

the consumed memory is16

the consumed time is31

===================================

class name is com.tn.list.Loop@1a758cb

the consumed memory is16

the consumed time is32

===================================

当size= 100

class name is com.tn.list.Enume@757aef

the consumed memory is56

the consumed time is16

===================================

class name is com.tn.list.Iter@61de33

the consumed memory is488

the consumed time is15

===================================

class name is com.tn.list.ListIter@ca0b6

the consumed memory is16

the consumed time is0

===================================

class name is com.tn.list.Loop@1a758cb

the consumed memory is272

the consumed time is16

===================================

当size=300000

class name is com.tn.list.Enume@757aef

the consumed memory is56

the consumed time is47

===================================

class name is com.tn.list.Iter@61de33

the consumed memory is12664

the consumed time is62

===================================

class name is com.tn.list.ListIter@ca0b6

the consumed memory is16

the consumed time is63

===================================

class name is com.tn.list.Loop@1a758cb

the consumed memory is272

the consumed time is47

===================================

当size =500000

class name is com.tn.list.Enume@757aef

the consumed memory is160

the consumed time is78

===================================

class name is com.tn.list.Iter@61de33

the consumed memory is16

the consumed time is110

===================================

class name is com.tn.list.ListIter@ca0b6

the consumed memory is272

the consumed time is93

===================================

class name is com.tn.list.Loop@1a758cb

the consumed memory is16

the consumed time is78

===================================

posted on 2011-08-11 13:09  布兜兜  阅读(640)  评论(0编辑  收藏  举报