Сравнение методов итерации по быстродействию

Snippet для консоли JShell. НЕ ЯВЛЯЕТСЯ нормальным бенчмарком. Для бенчмарков следует использовать, например, JMH.

import java.time.LocalDate;
import java.time.Period;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.TreeMap;
import java.util.stream.Collectors;

LocalDate startDate = LocalDate.of(2022, 11, 1).plusMonths(1);
LocalDate endDate = startDate.minusMonths(12);
static List<LocalDate> datesList = endDate.datesUntil(startDate, Period.ofMonths(1)).collect(Collectors.toList());
static LocalDate[] datesArray = endDate.datesUntil(startDate, Period.ofMonths(1)).toArray(LocalDate[]::new);
static TreeMap<Long, List<String>> results = new TreeMap<>();
long start, duration;

System.out.println("Dates list for - i");
start = System.currentTimeMillis();
for (int i = 0; i < datesList.size(); i++) {
    System.out.printf("\r %s", datesList.get(i));
}
duration = System.currentTimeMillis() - start;
System.out.printf("%n time: %d %n", duration);
results.computeIfAbsent(duration, k -> new ArrayList<>()).add("Dates list for - i");

System.out.println("Dates list for - each");
start = System.currentTimeMillis();
for (LocalDate date : datesList) {
    System.out.printf("\r %s", date);
}
duration = System.currentTimeMillis() - start;
System.out.printf("%n time: %d %n", duration);
results.computeIfAbsent(duration, k -> new ArrayList<>()).add("Dates list for - each");

System.out.println("Dates list forEach");
start = System.currentTimeMillis();
datesList.forEach(date -> System.out.printf("\r %s", date));
duration = System.currentTimeMillis() - start;
System.out.printf("%n time: %d %n", duration);
results.computeIfAbsent(duration, k -> new ArrayList<>()).add("Dates list forEach");

System.out.println("Dates list while + iterator");
start = System.currentTimeMillis();
static Iterator<LocalDate> iterator = datesList.iterator();
while (iterator.hasNext()) {
    System.out.printf("\r %s", iterator.next());
}
duration = System.currentTimeMillis() - start;
System.out.printf("%n time: %d %n", duration);
results.computeIfAbsent(duration, k -> new ArrayList<>()).add("Dates list while + iterator");

System.out.println("Dates array for - i");
start = System.currentTimeMillis();
for (int i = 0; i < datesArray.length; i++) {
    System.out.printf("\r %s", datesArray[i]);
}
duration = System.currentTimeMillis() - start;
System.out.printf("%n time: %d %n", duration);
results.computeIfAbsent(duration, k -> new ArrayList<>()).add("Dates array for - i");

System.out.println("Dates in-place generated for each");
start = System.currentTimeMillis();
for (LocalDate date = endDate; date.isBefore(startDate); date = date.plusMonths(1)) {
    System.out.printf("\r %s", date);
}
duration = System.currentTimeMillis() - start;
System.out.printf("%n time: %d %n", duration);
results.computeIfAbsent(duration, k -> new ArrayList<>()).add("Dates in-place generated for each");

results.forEach((key, value) -> value.forEach(testName -> System.out.println(testName + " => " + key)));