LinkedList versus ArrayList Performance
here's a little test to run to show the difference in performance between a LinkedList and an ArrayList. the test captures time taken to load 1,000,000 objects into each of the lists, followed by the time taken to add another object at index 0.here's the quick results; LinkedList is slower to load, but faster to add. the ArrayList was about 25% faster to load, but 5x slower to add. (these tests were run using JDK7 at Java 5 compatibility on a MacBook Pro late 2011 model)
package de.incompleteco.java; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import org.junit.Test; public class ListPerformanceTest { private List < String > values; @Test public void testArrayList() { values = new ArrayList < String > (); long startTime = System.currentTimeMillis(); for (int i=0;i < 10000000;i++) { values.add("hello" + i); }//end for long endTime = System.currentTimeMillis(); System.out.println("loading: " + (endTime - startTime)); //now get the start time startTime = System.currentTimeMillis(); //add to the beginnging values.add(0, "hello first"); //stop endTime = System.currentTimeMillis(); //show the difference System.out.println("adding: " + (endTime - startTime)); } @Test public void testLinkedList() { values = new LinkedList < String > (); long startTime = System.currentTimeMillis(); for (int i=0;i < 10000000;i++) { values.add("hello" + i); }//end for long endTime = System.currentTimeMillis(); System.out.println("loading: " + (endTime - startTime)); //now get the start time startTime = System.currentTimeMillis(); //add to the beginnging values.add(0, "hello first"); //stop endTime = System.currentTimeMillis(); //show the difference System.out.println("adding: " + (endTime - startTime)); } }
No comments:
Post a Comment