我有一个Stack类。
我正在进行性能测试。
我在增加负荷时看到奇怪的性能变化:
测试结果。
元素:500000;时间:18
元素:710000;时间:21
元素:720000;时间:193
元素:750000;时间:164
元素:1000000;时间:178
元素:1500000;时间:196
元素:2000000;时间:1875年
如何解释此类性能下降?
public class ListBackedStack<T> {
private int size;
private Entry currentEntry;
public ListBackedStack() {
}
public void push(final T data) {
Entry newEntry = new Entry(data);
newEntry.previous = currentEntry;
currentEntry = newEntry;
size++;
}
@SuppressWarnings("unchecked")
public T pop() {
T data = currentEntry.data;
currentEntry = currentEntry.previous;
size--;
return data;
}
public int size() {
return size;
}
private class Entry {
private Entry previous;
private T data;
private Entry(T data) {
this.data = data;
}
private void setPrevious(Entry previous) {
this.previous = previous;
}
}
}
public class ListStackTest {
@Before
public void before() {
stackList = new ListBackedStack<Object>();
}
@Test
public void testPerformanceListStack() {
long time = currentTime();
for (int i = 0; i < testDataSize; i++) {
stackList.push(i);
}
System.out.println("[StackList] Elements:" + testDataSize + "; time:" + timing(time));
stackList = null;
}
private long currentTime() {
return System.currentTimeMillis();
}
private long timing(long time) {
return (currentTime() - time);
}
private static final int testDataSize = 710000 /** Integer.MAX_VALUE */
;
private ListBackedStack<Object> stackList;
}