0

The answer might be obvious, however I still feel the need to ask. So I programmed a simple Linked_List for fun and training and noticed that one method that is supposed to print the elements of the list actually runs faster than the method that prints the list backward.

I kept thinking about a reason to explain this, but I kept going in circle, I would be interesting to know why.

Here is my first method (this is the simple traversing of the list)


public void list(){
    Node<E> iter = head;
        while(iter != null){
            System.out.print(iter.getValue() + ", ");
            iter = iter.getNext();
        }
}

Here is the other method, it prints the list backward


public void list_inverse(){
    Node<E> iter = head;
    Stack<Node<E>> stack = new Stack<Node<E>>();
        while(iter != null){
            stack.push(iter);
            iter = iter.getNext();
        }
        while(!stack.isEmpty()){
            Node<E> tmp = stack.peek();
            stack.pop();
            System.out.print(tmp.getValue() + ", ");
        }
}

so the idea of the second method is the go through the list and add each item to a stack. After reaching the end, I just print the element contained in the stack. So you can see, the second method was supposed to run in a longer period of time.


in the main method we have :

    long startTime = System.nanoTime();
    list.list();
    long ElapsedTime = System.nanoTime();
    System.out.println();
    long startTime2 = System.nanoTime();
    list.list_inverse();
    long ElapsedTime2 = System.nanoTime();

the output I had :


give the number for the list, if you want to stop, write stop
1
2
3
4
5
6
7
8
9
10
stop

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, //first method
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, // second method

it has taken 8.15E-5 second for list and 5.19E-5 second for list_inverse
j_v_wow_d
  • 511
  • 2
  • 11
Philippe
  • 700
  • 1
  • 7
  • 17
  • 4
    Please read the following: http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – NPE Dec 01 '14 at 22:38
  • 3
    There is no mystery. This is just another case of a poorly designed micro-benchmark giving misleading results. Hint: read NPE's link. – Stephen C Dec 01 '14 at 22:47
  • Ok, thank you for your answers, could I just ask you another question really fast ? I've began learning programming a couple of months ago only, and I would like to know when can you consider yourself an intermediate programmer and thus leave the beginner state ? – Philippe Dec 01 '14 at 22:58
  • 1
    @PhilMoesch there is really no clear definition of that. I'd say you're intermediate once you understand like 90% of the code you see. And after that when you believe to understand everything and start to believe you know it better you're "advanced" or "junior developer" in those terms here: http://programmers.stackexchange.com/questions/14914/whats-the-difference-between-entry-level-jr-sr-developers/14972#14972 a.k.a. you're still a beginner somehow :p – zapl Dec 02 '14 at 02:03

1 Answers1

0

Stack in Java uses Vector internally. As Vector uses array, it stores the elements in consecutive memory locations. Hence, the jvm doesn't need to go to the memory location pointed by the reference to retrieve the values. It can scan the next memory location and get the values.

I believe this is the main reason for difference in execution times for the first and second method. This is pure guesswork though ;-)

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • you overlook that the stack part is done in addition, adding things should not result in less than before :) – zapl Dec 01 '14 at 22:46
  • You are not calling getValue(), hence I believe it never goes to the memory location where the value is stored. – Darshan Mehta Dec 01 '14 at 22:48
  • 1
    And it is not the real explanation. The real explanation is that the benchmark is giving bogus results because it doesn't take proper account of JVM warmup, etc, etc. – Stephen C Dec 01 '14 at 22:49
  • As I said, this is pure guesswork. I have no clue how benchmarks were calculated. – Darshan Mehta Dec 01 '14 at 22:50
  • That would be `Node#getValue()`, the stack may store references to nodes in consecutive memory but the method had those references already so there is no gain possible. It would need to have an alternative way of getting to the value directly from consecutive memory. – zapl Dec 01 '14 at 22:50