Benchmarks
Benchmark performance of sequential vs parallel streams for statistics.
Benchmarks is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Benchmarks measure speed difference between sequential and parallel streams.
Setup
Use a large dataset (e.g., millions of numbers) and measure execution time for sum/average.
Code: sequential benchmark
This measures time for sequential sum.
import java.util.*;public class Main {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>();
for(int i=0;i<1_000_000;i++) nums.add(i);
long start = System.currentTimeMillis();
long sum = nums.stream().mapToLong(n -> n).sum();
long time = System.currentTimeMillis() - start;
System.out.println("Sequential sum=" + sum + " in " + time + "ms");
}
}
Code: parallel benchmark
This measures time for parallel sum. On large datasets, it should be faster.
import java.util.*;public class Main {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>();
for(int i=0;i<1_000_000;i++) nums.add(i);
long start = System.currentTimeMillis();
long sum = nums.parallelStream().mapToLong(n -> n).sum();
long time = System.currentTimeMillis() - start;
System.out.println("Parallel sum=" + sum + " in " + time + "ms");
}
}
Compare
Compare times: if data is large and CPU-bound, parallel is faster. For small lists, sequential may win.
Code: average benchmark
You can also benchmark averages to see speed difference.
import java.util.*;public class Main {
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>();
for(int i=0;i<1_000_000;i++) nums.add(i);
long start = System.currentTimeMillis();
double avg = nums.parallelStream().mapToInt(n -> n).average().orElse(0);
long time = System.currentTimeMillis() - start;
System.out.println("Parallel average=" + avg + " in " + time + "ms");
}
}
Benchmark check
Quick check: When do parallel streams usually show performance benefits?
Recap
Recap: Benchmarked sequential vs parallel streams. Parallel wins on large data, sequential may be faster for small.
Frequently asked questions
Is the “Benchmarks” lesson free?
Yes — the full text of “Benchmarks” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Benchmarks”?
Benchmark performance of sequential vs parallel streams for statistics. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Benchmarks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Design
- Implementation
- Benchmarks