Performance Comparison of Java for Loop, foreach Loop, and Stream API
This article benchmarks Java's traditional for loop, enhanced foreach loop, and Stream API by measuring execution time on 10,000 and 10,000,000 elements, showing that for loops are fastest for small datasets while Stream offers parallelism advantages for large data volumes.
In response to a discussion about the redundancy of Java 8 Stream methods compared to SQL functions, the author conducted performance tests to compare three iteration techniques in Java: the classic for loop, the enhanced foreach loop, and the Stream API.
for loop test (10,000 elements)
public class ForTest {
public static void main(String[] args) {
Long startTime = System.currentTimeMillis();
formMethod();
Long endTime = System.currentTimeMillis();
System.out.println("time_total:" + (endTime - startTime));
}
public static void formMethod() {
for (int i = 0; i < 10000; i++) {
System.out.println("__________for循环____________");
}
}
}The execution time repeatedly fell between 90 and 100 milliseconds, indicating the best performance among the three methods for this data size.
foreach loop test (10,000 elements)
public class ForTest {
public static void main(String[] args) {
List
lists = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
lists.add(i);
}
Long startTime = System.currentTimeMillis();
formMethod(lists);
Long endTime = System.currentTimeMillis();
System.out.println("time_total:" + (endTime - startTime));
}
public static void formMethod(List
lists) {
lists.forEach(i -> {
System.out.println("__________forEach____________");
});
}
}The measured time was around 150 milliseconds, noticeably slower than the plain for loop.
Stream API test (10,000 elements)
public class ForTest {
public static void main(String[] args) {
List
lists = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
lists.add(i);
}
Long startTime = System.currentTimeMillis();
formMethod(lists);
Long endTime = System.currentTimeMillis();
System.out.println("time_total:" + (endTime - startTime));
}
public static void formMethod(List
lists) {
lists.stream().forEach(i -> {
System.out.println("__________stream处理____________");
});
}
}The execution time was comparable to the enhanced foreach loop, showing no significant advantage for this small dataset.
Conclusions for datasets under 10,000 elements
★ For data volumes below 10,000, the classic for loop outperforms both foreach and Stream.
When the data size was increased to 10,000,000 elements, the timings for all three approaches converged (around 43,000 ms for for , similar for foreach and Stream). However, the Stream API offers a parallelStream() method that can leverage multiple CPU cores, potentially providing a large performance boost for massive datasets on multi‑core machines.
Final takeaway : Use the simple for loop for small to medium collections where raw speed matters; consider Stream (especially parallelStream ) when dealing with very large collections and you can benefit from parallel execution.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.