關於ArrayList,FastArrayList,TreeList,Vector,Stack的效能測試

package test.list;

import static java.lang.System.out;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.commons.collections.FastArrayList;
import org.apache.commons.collections.list.TreeList;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;

@SuppressWarnings("unchecked")
public class ListPerformance {
	public static void main(String[] args) {
		ListPerformance test = new ListPerformance(10*10000);
		out.print(StringUtils.center(
				"Test List Performance: loop=" + test.loop, 80, '-'));
		out.println("\n\t\tadd\tinsert\tremove\tget\tset\titerator\tfor");
		test.benchmark(new FastArrayList());
		test.benchmark(new TreeList());
		test.benchmark(new ArrayList());
		test.benchmark(new LinkedList());
		test.benchmark(new CopyOnWriteArrayList());
		test.benchmark(new Vector());
		test.benchmark(new Stack());
		// 2.测试排序
		out.print("\n\n");
		out.print(StringUtils.center("Test List sort Performance: loop="
				+ test.loop, 80, '-'));
		out.printf("\n\t\t\toptimize\tworst\trandom");
		test.benchmarkSort(new FastArrayList());
		test.benchmarkSort(new TreeList());
		test.benchmarkSort(new ArrayList());
		test.benchmarkSort(new LinkedList());
		 test.benchmarkSort(new
		 CopyOnWriteArrayList());//UnsupportedOperationException
		test.benchmarkSort(new Vector());
		test.benchmarkSort(new Stack());
		// 3.测试各种数据结构间转化
		out.print("\n\n");
		out.print(StringUtils.center("Test List convert Performance: loop="
				+ test.loop, 80, '-'));
		out.printf("\n\t\tTree\tArray\tLinked\tCopyOnWrite\tVector");
		test.benchmarkConvert(new FastArrayList());
		test.benchmarkConvert(new TreeList());
		test.benchmarkConvert(new ArrayList());
		test.benchmarkConvert(new LinkedList());
//		test.benchmarkConvert(new CopyOnWriteArrayList());
	}

	private int loop = 10000;

	public ListPerformance(int loop) {
		this.loop = loop;
	}

	public void benchmark(List list) {
		out.format(StringUtils.left("\n" + list.getClass().getSimpleName(), 15));
		int j;
		StopWatch watch = null;
		// 1.测试顺序性能(Add)
		(watch = new StopWatch()).start();
		for (int i = 0; i < loop; i++) {
			list.add(new Integer(i));
		}
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 2.测试随机插入性能(Random insert)
		(watch = new StopWatch()).start();
		for (int i = 0; i < loop; i++) {
			j = (int) (Math.random() * loop);
			list.add(j, new Integer(-j));
		}
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 3.测试随机索引删除(Random remove)
		(watch = new StopWatch()).start();
		for (int i = 0; i < loop; i++) {
			j = (int) (Math.random() * loop);
			list.remove(j);
		}
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 4.测试随机取数性能(Random get)
		(watch = new StopWatch()).start();
		for (int i = 0; i < loop; i++) {
			j = (int) (Math.random() * loop);
			list.get(j);
		}
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 5.测试随机更新性能(Random set)
		(watch = new StopWatch()).start();
		for (int i = 0; i < loop; i++) {
			j = (int) (Math.random() * loop);
			list.set(j, j);
		}
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 6.测试迭代性能(Iterator)
		(watch = new StopWatch()).start();
		Iterator<Object> iter = list.iterator();
		while (iter.hasNext()) {
			iter.next();
		}
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 7.测试迭代性能(Iterator)
		(watch = new StopWatch()).start();
		// Iterator<Object> iter = list.iterator();
		for (Object obj : list) {

		}
		watch.stop();
		out.printf("\t" + watch.getTime());
	}

	public void benchmarkConvert(List list) {
		out.format(StringUtils.left("\n" + list.getClass().getSimpleName(), 15));
		StopWatch watch = null;
		// 1.转TreeList
		(watch = new StopWatch()).start();
		new TreeList(list);
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 2.转ArrayList
		(watch = new StopWatch()).start();
		new ArrayList(list);
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 3.转LinkedList
		(watch = new StopWatch()).start();
		new LinkedList(list);
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 4.转CopyOnWriteArrayList
		(watch = new StopWatch()).start();
		new CopyOnWriteArrayList(list);
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 5.转Vector
		(watch = new StopWatch()).start();
		new Vector(list);
		watch.stop();
		out.printf("\t" + watch.getTime());
	}

	public void benchmarkSort(List list) {
		out.format(StringUtils.left("\n" + list.getClass().getSimpleName(), 15));
		StopWatch watch = null;
		// 1.顺序List
		for (int i = 0; i < loop; i++) {
			list.add(new Integer(i));
		}
		(watch = new StopWatch()).start();
		Collections.sort(list);
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 2.逆序List
		for (int i = loop - 1; i > 0; i--) {
			list.add(new Integer(i));
		}
		(watch = new StopWatch()).start();
		Collections.sort(list);
		watch.stop();
		out.printf("\t" + watch.getTime());
		// 3.随机顺序List
		for (int i = 0, j = 0; i < loop; i++) {
			j = (int) (Math.random() * loop);
			list.add(new Integer(j));
		}
		(watch = new StopWatch()).start();
		Collections.sort(list);
		watch.stop();
		out.printf("\t" + watch.getTime());
	}
}

關於其中list 的測試結果。如下:(其中,測試數據量爲:=100000)

Class add Insert remove get set iterator for
FastArrayList 14 7356 7391 8 17 4 3
TreeList 77 154 123 39 41 6 3
ArrayList 20 7275 7264 6 18 3 3
LinkedList 41 109875 164135 94343 67299 2 1
CopyOnWriteArr 8049 58681 69310 7 44581 2 1
Vector 8 8623 20302 6 30 2 2
Stack 2 7227 7372 6 6 2 2
,以上表明:

在Iterator,和for的效能,各種list 是沒差的。並且很迅速。

單純的ArrayList 在add,get,set 就夠用,效能也算不錯。

TreeList 在add,insert,romove,get,set 上都有不俗的表現,其取決於其數據結構。

LinkedList ,由於數據結構爲鏈表形式,其數據結構限制了LinkedList 的查找效率,造成get()無效率,insert,romove 都需要用到查找方法,故結果這麼慢。

Vector 基於線程安全的,不做解釋。

FastArrayList 和ArrayList ,個人覺得没查。





你可能感兴趣的:(關於ArrayList,FastArrayList,TreeList,Vector,Stack的效能測試)