Java Collections – ArrayList vs LinkedList Performance

For this test, I decided to evaluate LinkedList vs ArrayList and see which one is fastest once and for all for the basic operations of add(), get() and remove(). From the tests I performed, it appears that LinkedList is quite a bit faster than ArrayList especially as the size of the collection grows.

Benchmark Leaders

  • #1. LinkedList
  • #2. ArrayList

Benchmark ArrayList vs. LinkedList (# iterations/Sec)

ArrayList_vs_LinkedLink2

Full Program Listing

package com.avaldes.tutorials;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LinkedListVsArrayBenchmark {

  public static void testIterations(List list, int length) {
    Object obj = new Object();
    for (int i=0; i<length; i++) list.add(obj);
    long startTime = System.currentTimeMillis();
    int iterations = 0;
    while (System.currentTimeMillis()-startTime < 1000) {
      iterations++;
      list.add(obj);
      list.get(5);
      list.remove(3);
    }
    System.out.println(list.getClass()+ " (" + length 
          + "), iterations: " + iterations);
  }
  
  public static void main(String[] args) {
    
    testIterations(new ArrayList(), 1000);
    testIterations(new ArrayList(), 10000);
    testIterations(new ArrayList(), 100000);
    testIterations(new ArrayList(), 1000000);
    testIterations(new ArrayList(), 10000000);
    
    testIterations(new LinkedList(), 1000);
    testIterations(new LinkedList(), 10000);
    testIterations(new LinkedList(), 100000);
    testIterations(new LinkedList(), 1000000);
    testIterations(new LinkedList(), 10000000);
  }
}

Output

class java.util.ArrayList (1000), iterations: 1196419
class java.util.ArrayList (10000), iterations: 163545
class java.util.ArrayList (100000), iterations: 15098
class java.util.ArrayList (1000000), iterations: 625
class java.util.ArrayList (10000000), iterations: 64
class java.util.LinkedList (1000), iterations: 9061265
class java.util.LinkedList (10000), iterations: 18942521
class java.util.LinkedList (100000), iterations: 24066964
class java.util.LinkedList (1000000), iterations: 18879194
class java.util.LinkedList (10000000), iterations: 19924783

Other Related Posts

Map Examples

List Examples

Set Examples

Please Share Us on Social Media

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *