📄 abstractsettest.java
字号:
package net.betterjava.collection.performance.set;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
import net.betterjava.util.BatchTest;
public abstract class AbstractSetTest extends BatchTest {
private final int indexOfRoundRpts = 3; //the index of inner loop in rpts
private int factor = 1; //the mutliple factor od inner loop
private int rpts[][] = { { 1000, 1000, 1000, 100 }, {
100, 100, 100, 2000 }, {
100, 100, 100, 4000 }, {
100, 100, 100, 10000 }, {
100, 100, 100, 20000 }
}; //the number of outer and inner loop
public AbstractSetTest(String file) {
super(5, 3, file);
}
public AbstractSetTest() {
super(5, 3);
}
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 outerSet = null;
switch (currentRound) {
case 0 :
outerSet = new HashSet();
break;
case 1 :
outerSet = new LinkedHashSet();
break;
case 2 :
outerSet = new TreeSet();
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++) {
Set innerSet;
try {
innerSet = (Set) outerSet.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], innerSet);
innerSet = null;
}
outerSet = null;
return String.valueOf(
elapse
* 1.0
/ (factor
* rpts[currentTurn][currentRound]
* rpts[currentTurn][indexOfRoundRpts]));
}
protected abstract long runTestBody(int rpts, Set innerSet);
protected void setFactor(int factor) {
this.factor = factor;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -