Upload
hujak-hrvatska-udruga-java-korisnika-croatian-java-user-association
View
97
Download
1
Embed Size (px)
Citation preview
Java lambdas and streams –are they better than for
loops?Aleksander Radovan, King ICT
Branko Mihaljević, HUJAK
Lambda advantages
• Functional programming paradigm
• Easier to distribute processing of collections over multiple threads(CPU cores)
• Stream API
• Faster than conventional loops?
Benchmarking hardware and methodology
• CPU: Intel Core i7-4702MQ (4 cores)
• 32 GB RAM
• SSD Samsung 850 EVO 500 GB
• Eclipse Neon
• Java 8 update 121
• Generating objects with random values
• Comparing times needed to finish a specific tasks with iterators, loop, comparators, sequential and parallel streams
• Every measurement was performed 10 times, the average values were taken intoconsiderations
1. Case: finding the youngest programmer (10 objects)
• Results (ms):
0,1
0,4
0,9
4,5
2,7
0 1 2 3 4 5
Iterator
ForLoop
Comparator
Sequential Stream
Parallel Stream
Finding the youngest programmer(10 objects)
1. Case: finding the youngest programmer (100 objects)
• Results (ms):
0,4
0,4
1,4
5,2
2,9
0 1 2 3 4 5 6
Iterator
ForLoop
Comparator
Sequential Stream
Parallel Stream
Finding the youngest programmer (100 objects)
1. Case: finding the youngest programmer (1.000 objects)
• Results (ms):
0,9
0,9
2,7
5,4
2,7
0 1 2 3 4 5 6
Iterator
ForLoop
Comparator
Sequential Stream
Parallel Stream
Finding the youngest programmer (1000 objects)
1. Case: finding the youngest programmer (10.000 objects)
• Results (ms):
3,1
2,7
11,4
7,8
4,3
0 2 4 6 8 10 12
Iterator
ForLoop
Comparator
Sequential Stream
Parallel Stream
Finding the youngest programmer (10.000 objects)
1. Case: finding the youngest programmer (100.000 objects)
• Results (ms):
14
14,9
79,7
22,4
10,3
0 20 40 60 80 100
Iterator
ForLoop
Comparator
Sequential Stream
Parallel Stream
Finding the youngest programmer (100.000 objects)
1. Case: finding the youngest programmer(1.000.000 objects)
• Results (ms):
39,3
33,1
732,7
79,3
43,1
0 200 400 600 800
Iterator
ForLoop
Comparator
Sequential Stream
Parallel Stream
Finding the youngest programmer (1.000.000 objects)
1. Case: finding the youngest programmer(summary)
• Results (ms):
0
20
40
60
80
100
10 100 1000 10000 100000 1000000
Finding the youngest programmer (without Comparator solution)
Iterator ForLoop Sequential Stream Parallel Stream
2. Case: finding programmer with the highest salary(summary)
• Results (ms):
0
10
20
30
40
10 100 1000 10000 100000
Finding programer with the highest salary (without Comparator solution)
Iterator ForLoop Sequential Stream Parallel Stream
3. Case: filtering programmers by lastname
• Results (ms):
0
5
10
15
20
25
30
10 100 1000 10000 100000
Filtering programmers by lastname
Iterator ForLoop Sequential Stream Parallel Stream
4. Case: grouping programmers by programminglanguage
• Results (ms):
0
20
40
60
80
100
120
10 100 1000 10000 100000 1000000
Grouping the objects into a map
Iterator ForLoop Sequential Stream
Conclusions
• Stream API methods not the best choice in every situation
• In case of arrays (instead of collections), the difference is more drastic
• With fewer objects (< 1000) the conventional (older) way is more appropriate
• The object type is also important (comparing BigDecimals, LocalDates…)
• The streams have certain amount of overhead
• But, „one-liner-programming” is fancy