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

📄 abstractmaptest.java

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

import net.betterjava.util.BatchTest;
import java.util.Map;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.TreeMap;

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

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

	public AbstractMapTest() {
		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);
	}

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

		switch (currentRound) {
			case 0 :
				outerMap = new HashMap();
				break;
			case 1 :
				outerMap = new Hashtable();
				break;
			case 2 :
				outerMap = new Hashtable();
				break;
			case 3 :
				outerMap = new TreeMap();
				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++) {
			Map innerMap;
			try {
				innerMap = (Map) outerMap.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], innerMap);
			innerMap = null;
		}
		outerMap = null;

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

	protected abstract long runTestBody(int rpts, Map innerMap);

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

}

⌨️ 快捷键说明

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