Here’s a complete, runnable Java program that benchmarks ArrayList vs LinkedList for add, get, and remove operations.
It uses System.nanoTime() for timing and handles edge cases like empty lists.

import java.util.ArrayList;import java.util.LinkedList;import java.util.List;public class ListPerformanceBenchmark {    private static final int ELEMENT_COUNT = 100_000;    public static void main(String[] args) {        benchmark("ArrayList", new ArrayList<>());        benchmark("LinkedList", new LinkedList<>());    }    private static void benchmark(String name, List<Integer> list) {        System.out.println("=== " + name + " Benchmark ===");        // Measure add at end long start = System.nanoTime();        for (int i = 0; i < ELEMENT_COUNT; i++) {            list.add(i);        }        long end = System.nanoTime();        System.out.printf("Add at end: %.2f ms%n", (end - start) / 1_000_000.0);        // Measure get from random positions        start = System.nanoTime();        for (int i = 0; i < ELEMENT_COUNT; i += 1000) {            list.get(i);        }        end = System.nanoTime();        System.out.printf("Random get: %.2f ms%n", (end - start) / 1_000_000.0);        // Measure remove from start        start = System.nanoTime();        for (int i = 0; i < ELEMENT_COUNT / 10; i++) { // remove fewer to avoid huge cost            list.remove(0);        }        end = System.nanoTime();        System.out.printf("Remove from start: %.2f ms%n", (end - start) / 1_000_000.0);        System.out.println();    }}

What This Does

  1. Add at end β†’ Measures how fast each list appends elements.
  2. Random get β†’ Measures random access speed.
  3. Remove from start β†’ Measures how fast elements are removed from the beginning.

Expected Results (Typical)

Operation
ArrayList
LinkedList
Add at end
Fast
Fast
Random get
Very fast
Slow
Remove from start
Slow
Fast

πŸ’‘ Why?

  • ArrayList: Backed by a dynamic array β†’ fast random access, but shifting elements on removal is costly.
  • LinkedList: Backed by nodes β†’ fast insert/remove at ends, but slow random access.



here’s an extended Java benchmark that compares ArrayList and LinkedList for:

  • Add at end
  • Add in middle
  • Random get
  • Remove from start
  • Iteration performance

It’s complete, runnable, and safe.

Java
コードをコピー
import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Iterator;public class ExtendedListBenchmark {    private static final int ELEMENT_COUNT = 100_000;    public static void main(String[] args) {        benchmark("ArrayList", new ArrayList<>());        benchmark("LinkedList", new LinkedList<>());    }    private static void benchmark(String name, List<Integer> list) {        System.out.println("=== " + name + " Benchmark ===");        // 1. Add at end long start = System.nanoTime();        for (int i = 0; i < ELEMENT_COUNT; i++) {            list.add(i);        }        long end = System.nanoTime();        System.out.printf("Add at end: %.2f ms%n", (end - start) / 1_000_000.0);        // 2. Add in middle        start = System.nanoTime();        for (int i = 0; i < 1000; i++) {            list.add(list.size() / 2, -1);        }        end = System.nanoTime();        System.out.printf("Add in middle (1000 ops): %.2f ms%n", (end - start) / 1_000_000.0);        // 3. Random get        start = System.nanoTime();        for (int i = 0; i < ELEMENT_COUNT; i += 1000) {            list.get(i);        }        end = System.nanoTime();        System.out.printf("Random get: %.2f ms%n", (end - start) / 1_000_000.0);        // 4. Remove from start        start = System.nanoTime();        for (int i = 0; i < ELEMENT_COUNT / 10; i++) {            list.remove(0);        }        end = System.nanoTime();        System.out.printf("Remove from start (10%% of list): %.2f ms%n", (end - start) / 1_000_000.0);        // 5. Iteration        start = System.nanoTime();        Iterator<Integer> iterator = list.iterator();        while (iterator.hasNext()) {            iterator.next();        }        end = System.nanoTime();        System.out.printf("Iteration over list: %.2f ms%n", (end - start) / 1_000_000.0);        System.out.println();    }}

What’s New

  1. Add in middle β†’ Shows how costly inserting in the middle is for ArrayList (shifting elements) vs LinkedList (traversal cost).
  2. Iteration β†’ Measures sequential traversal speed.
  3. Reduced middle insertions to 1000 to avoid excessive runtime.

Typical Results

Operation
ArrayList
LinkedList
Add at end
Fast
Fast
Add in middle
Slow
Moderate
Random get
Very fast
Very slow
Remove from start
Slow
Fast
Iteration
Fast
Slightly slower

πŸ’‘ Key Takeaways:

  • Use ArrayList for frequent reads and append operations.
  • Use LinkedList for frequent insertions/removals at the start or middle.
  • Iteration is generally fast for both, but ArrayList often wins due to better cache locality.
← Back to Learning Journey