0

So I've been tasked an assignment to compare LinkedLists and ArrayLists for two different things.

part 1) randomly select locations inside the list and increment those values by 1 for each list type

part 2) double each list size by adding random locations, and then immediately remove the same amount of random locations bring the list down to original size.

I feel I've accomplished this pretty well but my teacher is saying part 1 ArrayList should be faster (which it is in my code). He is saying LinkedList should be wayyy faster in part 2, which its the exact opposite for me... its way slower.. I've adding in SYSO's at different locations to verify that the lists are being modified correctly and everything and cant figure out why its not working out the way he says it should be.

can anyone spot what I've done wrong (if anything?). Thank you so much

import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import javax.swing.JOptionPane; public class LinkedListVersusArrayList { public static void main(String[] args) { long startTime, endTime, duration; List<Double> LL = new LinkedList<Double>(); ArrayList<Double> AL = new ArrayList<Double>(); int size = Integer.parseInt(JOptionPane.showInputDialog("Pick a list size (whole number only please)")); //fills the Linked List with random doubles for (int i = 0; i < size; i++){ LL.add(Math.random()); } //fills the ArrayList with random doubles for (int i = 0; i < size; i++){ AL.add(Math.random()); } // //Part 1 // System.out.println("\nPART 1:\nBoth lists are now full of random numbers. \nI will now cycle through " + "and incremiment random locations " +size+ " times for each list.\n"); //testing the LinkedList first for random access startTime = System.nanoTime(); for (int i = 0; i < size; i++){ int x = (int)(LL.size()*Math.random()); double y = LL.get(x); LL.set(x, y+1); } endTime = System.nanoTime(); duration = (endTime - startTime); System.out.println("Linked List took: " +(duration/1000000) +" milli seconds"); //testing the ArrayList now for random access startTime = System.nanoTime(); for (int i = 0; i < size; i++){ int x = (int)(AL.size()*Math.random()); double y = AL.get(x); AL.set(x, y+1); } endTime = System.nanoTime(); duration = (endTime - startTime); System.out.println("Array List took: " +(duration/1000000) +" milli seconds"); // //Part 2 // System.out.println("\nPART 2:\nBoth lists will now get "+size+" slots added to them in random locations.\n" + "After this is complete, we will remove "+size+" slots from each list at random\n"); //testing the LinkedList first for random adding/subtracting startTime = System.nanoTime(); //add for (int i = 0; i < size; i++){ int x = (int)(LL.size()*Math.random()); LL.add(x, 1.0); } //delete for (int i = 0; i < size; i++){ int x = (int)(LL.size()*Math.random()); LL.remove(x); } endTime = System.nanoTime(); duration = (endTime - startTime); System.out.println("Linked List took: " +(duration/1000000) +" milli seconds"); //testing the ArrayList now for random adding/subtracting startTime = System.nanoTime(); //add for (int i = 0; i < size; i++){ int x = (int)(AL.size()*Math.random()); AL.add(x, 1.0); } //delete for (int i = 0; i < size; i++){ int x = (int)(AL.size()*Math.random()); AL.remove(x); } endTime = System.nanoTime(); duration = (endTime - startTime); System.out.println("Array List took: " +(duration/1000000) +" milli seconds"); } 

}

11
  • what exactly do you mean when you say "double each list size by adding random locations"?
    – bhspencer
    CommentedJul 14, 2015 at 20:32
  • adding elements. so if the arraylist has a size of 200 initially, after doubling it, it will have 400 elements. just by adding to random locations inside of the array
    – DojoOria
    CommentedJul 14, 2015 at 20:33
  • Doing micro benchmarks is non-trivial, especially on the JVM (because of the runtime's optimizing compiler). Theoretically, random adds on a linked list should be faster than on an ArrayList (Since the ArrayList will shift all elements right of the position), but the difference is not huge (since a linked list must scan through the list to find pos X) - especially for small lists.
    – folkol
    CommentedJul 14, 2015 at 20:35
  • I don't see anything wrong with your code. I suspect the problem is that you are testing with numbers of items that are far too small to matter in your test results. Try adding 10 million items rather than 200. And repeat the test 10 times are running it once to warm up the JVM.
    – bhspencer
    CommentedJul 14, 2015 at 20:37
  • I've tested up to 50,000 so far. I'm running a test currently with 500,000 and it seems to be stuck on part 2.
    – DojoOria
    CommentedJul 14, 2015 at 20:39

1 Answer 1

3

Apart from all the microbenchmarking issues which your code doesn't control for, your teacher's expectations for case 2 are also wrong. He is disregarding the cost of accessing a random element of LinkedList (the prerequisite to inserting at that spot) and overestimating the cost of inserting into the ArrayList. The cost of copying contiguous blocks of memory is much lower than the cost of chasing a long chain of pointers, which is what is required to access a random element of the LinkedList.

In reality LinkedList has a very narrow area of application where it is any improvement over ArrayList. For example, you could try to insert only near the front of the list. This way you'll emphasize the cost of moving elements in ArrayList and de-emphasize the cost of accessing a member of LinkedList.

3
  • do you mean by micro bench marking, the fact that I'm not accessing the same spots in order for each list for each test... i considered generating a random list of numbers (size long) and then using that for the iterations of the for loops. I thought that would be more "fair" but my teach said that shouldn't be necessary
    – DojoOria
    CommentedJul 14, 2015 at 20:53
  • I mean you don't warm up the code, you reuse the same JVM for all tests, you have all tests in line in the same method (precludes some optimizations), and many more. Microbenchmarking on a JIT-compiled, managed runtime is extremely deceptive.CommentedJul 14, 2015 at 20:59
  • thank you very much for the in dept response. I'm afraid at my level (1.5 classes into my programming career) this is a little over my head to say the least. I'm going to put some research into this and what you have said though. Thank you for the help! I really appreciate it
    – DojoOria
    CommentedJul 14, 2015 at 21:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.