I have a Stack class below.
I'm doing performance test for it.
I see strange perforamance change when increasing load:
Test results.
Elements:500000; time:18
Elements:710000; time:21
Elements:720000; time:193
Elements:750000; time:164
Elements:1000000; time:178
Elements:1500000; time:196
Elements:2000000; time:1875
How to explain such performance degradation ?
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;
}