⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractlisttest.java

📁 高质量Java程序设计 源代码
💻 JAVA
字号:
package net.betterjava.collection.performance.list;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.Vector;

import net.betterjava.util.BatchTest;

public abstract class AbstractListTest extends BatchTest {
	private int indexOfRoundRpts = 4; //the index of inner loop in rpts
	private int factor = 1; //the mutliple factor od inner loop
	private int rpts[][] = { { 10000, 1000, 1000, 1000, 100 }, {
			2000, 1000, 1000, 1000, 200 }, {
			500, 100, 100, 100, 500 }, {
			200, 100, 100, 100, 1000 }, {
			100, 10, 10, 10, 10000 }
	}; //the number of outer and inner loop

	public AbstractListTest(String file) {
		super(5, 4, file);
	}

	public AbstractListTest() {
		super(5, 4);
	}

	protected void setLoopTable(int[][] table) {
		if (rpts.length == 0 || rpts[0].length == 0)
			throw new IllegalArgumentException("empty loop table");

		rpts = table;
		this.setTurn(rpts.length);
		this.setRound(rpts[0].length - 1);
		indexOfRoundRpts = rpts[0].length - 1;
	}

	protected String round(int currentTurn, int currentRound) {
		Object outerList = null;

		switch (currentRound) {
			case 0 :
				outerList = new LinkedList();
				break;
			case 1 :
				outerList = new ArrayList();
				break;
			case 2 :
				outerList = new Vector();
				break;
			case 3 :
				outerList = new Stack();
				break;
			default :
				throw new IllegalArgumentException(
					"currentRound:" + String.valueOf(currentRound));
		}

		log(
			"outer repeats "
				+ rpts[currentTurn][currentRound]
				+ " inner repeats "
				+ factor * rpts[currentTurn][indexOfRoundRpts]);
		long elapse = 0;
		for (int i = 0; i < rpts[currentTurn][currentRound]; i++) {
			List innerList;
			try {
				innerList = (List) outerList.getClass().newInstance();
			} catch (InstantiationException e1) {
				log(e1);
				throw new IllegalStateException("cast error");
			} catch (IllegalAccessException e2) {
				log(e2);
				throw new IllegalStateException("cast error");
			}
			elapse
				+= runTestBody(factor * rpts[currentTurn][indexOfRoundRpts], innerList);
			innerList = null;
		}
		outerList = null;

		return String.valueOf(
			elapse
				* 1.0
				/ (factor
					* rpts[currentTurn][currentRound]
					* rpts[currentTurn][indexOfRoundRpts]));
	}

	protected abstract long runTestBody(int rpts, List innerList);

	protected void setFactor(int factor) {
		this.factor = factor;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -