Update, May 2026: the original benchmark below should not be treated as evidence that a classic for loop is meaningfully faster than foreach. It used System.currentTimeMillis(), no warmup, no repeated forks, and loops whose work could be optimized away. A proper Java microbenchmark should use JMH and should consume real work so the JIT cannot remove the measurement target.

This post is kept as a learning snapshot, but the conclusion has changed: prefer the loop that makes the code clearer unless a production measurement with representative data shows otherwise.

Transcribed from the original LinkedIn image post.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.ArrayList;
import java.util.List;

public class Day06 {
    public static void main(String args[]) {
        List<String> fileTypeList = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            fileTypeList.add("fileType");
        }

        long beforeForLoop = System.currentTimeMillis();
        for (int i = 0; i < fileTypeList.size(); i++) {
            fileTypeList.get(i);
        }
        long afterForLoop = System.currentTimeMillis();
        System.out.println("Time took in millis for for " + (afterForLoop - beforeForLoop));

        long beforeForeachLoop = System.currentTimeMillis();
        for (String s : fileTypeList) {
        }

        long afterForeachLoop = System.currentTimeMillis();
        System.out.println("Time took in millis for foreach " + (afterForeachLoop - beforeForeachLoop));
    }
}

What a better benchmark needs

A useful version of this test needs:

  • JMH warmup and measurement iterations.
  • Several forks so one JVM run does not dominate the result.
  • Real work inside each loop.
  • A consumed result so the JIT cannot remove the loop.
  • Representative data structures, because arrays, ArrayList, LinkedList, and custom collections do not behave the same way.

For this series, Day 69 covers JMH in more detail: Day 69 - Unlocking Java Performance Secrets.

The original LinkedIn graphic is preserved below.

Day 6 LinkedIn post