-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStreamMapPerformanceTest.java
More file actions
71 lines (60 loc) · 2.52 KB
/
StreamMapPerformanceTest.java
File metadata and controls
71 lines (60 loc) · 2.52 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.pal.poc.post.contents.streams;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class StreamMapPerformanceTest {
private static final int ITERATIONS = 1000;
private static final int LIST_SIZE = 100_000;
public static void main(String[] args) {
List<Integer> numbers = IntStream.range(0, LIST_SIZE)
.boxed()
.collect(Collectors.toList());
for (int i = 0; i < 100; i++) {
multipleMapOperations(numbers);
combinedMapOperation(numbers);
}
// Test multiple map operations
long startMultiple = System.nanoTime();
for (int i = 0; i < ITERATIONS; i++) {
multipleMapOperations(numbers);
}
long multipleTime = (System.nanoTime() - startMultiple) / ITERATIONS;
// Test combined map operation
long startCombined = System.nanoTime();
for (int i = 0; i < ITERATIONS; i++) {
combinedMapOperation(numbers);
}
long combinedTime = (System.nanoTime() - startCombined) / ITERATIONS;
System.out.printf("Multiple maps average time: %,d ns%n", multipleTime);
System.out.printf("Combined map average time: %,d ns%n", combinedTime);
System.out.printf("Overhead ratio: %.2fx%n", (double) multipleTime / combinedTime);
System.out.println("\nIntermediate Objects Demo:");
numbers.stream()
.limit(3)
.map(n -> {
System.out.println("First map: " + n);
return n * 2;
})
.map(n -> {
System.out.println("Second map: " + n);
return n + 1;
})
.map(n -> {
System.out.println("Third map: " + n);
return n * n;
})
.collect(Collectors.toList());
}
private static List<Integer> multipleMapOperations(List<Integer> numbers) {
return numbers.stream()
.map(n -> n * 2) // First transformation
.map(n -> n + 1) // Second transformation
.map(n -> n * n) // Third transformation
.collect(Collectors.toList());
}
private static List<Integer> combinedMapOperation(List<Integer> numbers) {
return numbers.stream()
.map(n -> (n * 2 + 1) * (n * 2 + 1)) // Combined transformation
.collect(Collectors.toList());
}
}