0

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;
}
magulla
  • 499
  • 1
  • 9
  • 22
  • 1
    Read this: https://code.google.com/p/caliper/wiki/JavaMicrobenchmarks – caskey Jan 29 '15 at 02:06
  • A shot in the dark, but my guess would be that as you need more and more memory, so increases the reads and writes to the disk. – Fjotten Jan 29 '15 at 02:12
  • @Fjotten Elements:710000; time:21 Elements:720000; time:193. why there is 10 times jump. – magulla Jan 29 '15 at 02:15
  • Why would you ever push three quarters of a million elements onto a stack, and why do you care whether it takes 21ms or 193ms? – user207421 Jan 29 '15 at 03:19
  • You should not use stack data structure for this amount of data. Stack structure is efficient only when data going in first is required at the last. inserting millions of records and retrieving them back is not a strength of stack. – Nachiket Kate Jan 29 '15 at 08:32
  • @NachiketKate your comment is completely worng in terms of data structure. There is no limitations for stack. Also it's wrong in terms of my question. Please adivce why do I have this degradation jump ? :) – magulla Jan 29 '15 at 12:49
  • 1
    Yes, comment is not in terms of your question but it is about your approach, which is wrong. First question comes to find when everyone sees your question is "what is purpose or goal of this?" If you just want to find out how efficient stack is, then well answer is "it depends on lot of factors" (your machine hw, implementation, type of data etc.) Another thing, I am not sure how much you are well with data structures but stack data structure has some pros and cons. Size falls in cons. My advice is instead of loading stack with values, find out needs first and decide DS later. – Nachiket Kate Jan 29 '15 at 14:22
  • Since you are creating a lot of medium lived objects and garbage, it is quite likely you are incurring GC pauses. I suggest you run with `-verbose:gc` to see how long is spent in GC. – Peter Lawrey Jan 29 '15 at 21:42
  • @PeterLawrey thanks, you looks to be right about GC. Look to be an option for me to allocate more memory for JVM. I'll play and respond. – magulla Jan 30 '15 at 13:41
  • @Nachiket Kate read question again. My imple is one of two possible classical for stack (unless I have a bug). Stack is backed with "array" or with "single linked list".I have last. Ideally it should have constant insertation time, it means that time to insert 710000 items and 720000 should not differ in 10 times. – magulla Jan 30 '15 at 13:51

0 Answers0