Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in...
Transcript of Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in...
![Page 1: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/1.jpg)
Java 8 SearchWithParallelStreams
Example (Part 2)
Douglas C. [email protected]
www.dre.vanderbilt.edu/~schmidt
Professor of Computer Science
Institute for Software
Integrated Systems
Vanderbilt University
Nashville, Tennessee, USA
![Page 2: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/2.jpg)
2
Learning Objectives in this Part of the Lesson• Know how Java 8 parallel streams are
applied in SearchWithParallelStreams
• Understand the pros & cons of the SearchWithParallelStreams class
See SearchStreamGang/src/main/java/livelessons/streamgangs/SearchWithParallelStreams.java
![Page 3: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/3.jpg)
3
Pros of the SearchWithParallelStreams Class
![Page 4: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/4.jpg)
4
• This example shows that the difference between sequential & parallel streams is often minuscule!
See docs.oracle.com/javase/tutorial/collections/streams/parallelism.html
Pros of the SearchWithParallelStreams Class
![Page 5: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/5.jpg)
5
• This example shows that the difference between sequential & parallel streams is often minuscule!
List<List<SearchResults>>
processStream() {
return getInput()
.stream()
.map(this::processInput)
.collect(toList());
}Here’s processStream() from SearchWithSequentialStream
that we examined earlier
Pros of the SearchWithParallelStreams Class
![Page 6: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/6.jpg)
6
• This example shows that the difference between sequential & parallel streams is often minuscule!
List<List<SearchResults>>
processStream() {
return getInput()
.stream()
.map(this::processInput)
.collect(toList());
}
List<List<SearchResults>>
processStream() {
return getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
}
vs
Here’s processStream() in SearchWithParallelStreams
Pros of the SearchWithParallelStreams Class
![Page 7: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/7.jpg)
7
• This example shows that the difference between sequential & parallel streams is often minuscule!
List<List<SearchResults>>
processStream() {
return getInput()
.stream()
.map(this::processInput)
.collect(toList());
}
vs
List<List<SearchResults>>
processStream() {
return getInput()
.parallelStream()
.map(this::processInput)
.collect(toList());
}
Changing all the stream() calls to parallelStream() calls is the minusculedifference between implementations!!
Pros of the SearchWithParallelStreams Class
![Page 8: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/8.jpg)
8
Starting SearchStreamGangTestPARALLEL_SPLITERATOR executed in 409 msecsCOMPLETABLE_FUTURES_INPUTS executed in 426 msecsCOMPLETABLE_FUTURES_PHASES executed in 427 msecsPARALLEL_STREAMS executed in 437 msecsPARALLEL_STREAM_PHASES executed in 440 msecsRXJAVA_PHASES executed in 485 msecsPARALLEL_STREAM_INPUTS executed in 802 msecsRXJAVA_INPUTS executed in 866 msecsSEQUENTIAL_LOOPS executed in 1638 msecsSEQUENTIAL_STREAM executed in 1958 msecsEnding SearchStreamGangTest
• This example shows that the difference between sequential & parallel streams is often minuscule!
• Moreover, substantial speedups canoccur on multi-core processors!
Tests conducted on a 2.7GHz quad-core Lenovo P50 with 32 Gbytes of RAM
Pros of the SearchWithParallelStreams Class
45,000+ phrases
Search Phrases
Input Strings to Search
…
![Page 9: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/9.jpg)
9
Starting SearchStreamGangTestPARALLEL_SPLITERATOR executed in 369 msecsPARALLEL_STREAMS executed in 373 msecsCOMPLETABLE_FUTURES_INPUTS executed in 377 msecsCOMPLETABLE_FUTURES_PHASES executed in 383 msecsPARALLEL_STREAM_PHASES executed in 385 msecsRXJAVA_PHASES executed in 434 msecsPARALLEL_STREAM_INPUTS executed in 757 msecsRXJAVA_INPUTS executed in 774 msecsSEQUENTIAL_LOOPS executed in 1485 msecsSEQUENTIAL_STREAM executed in 1578 msecsEnding SearchStreamGangTest
• This example shows that the difference between sequential & parallel streams is often minuscule!
• Moreover, substantial speedups canoccur on multi-core processors!
Tests conducted on a 2.9GHz quad-core MacBook Pro with 16 Gbytes of RAM
Pros of the SearchWithParallelStreams Class
45,000+ phrases
Search Phrases
Input Strings to Search
…
![Page 10: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/10.jpg)
10
Starting SearchStreamGangTestPARALLEL_SPLITERATOR executed in 369 msecsPARALLEL_STREAMS executed in 373 msecsCOMPLETABLE_FUTURES_INPUTS executed in 377 msecsCOMPLETABLE_FUTURES_PHASES executed in 383 msecsPARALLEL_STREAM_PHASES executed in 385 msecsRXJAVA_PHASES executed in 434 msecsPARALLEL_STREAM_INPUTS executed in 757 msecsRXJAVA_INPUTS executed in 774 msecsSEQUENTIAL_LOOPS executed in 1485 msecsSEQUENTIAL_STREAM executed in 1578 msecsEnding SearchStreamGangTest
• This example shows that the difference between sequential & parallel streams is often minuscule!
• Moreover, substantial speedups canoccur on multi-core processors!
• Superlinear speed-ups arise from“hyper-threaded” (virtual) cores
See en.wikipedia.org/wiki/Hyper-threading
Pros of the SearchWithParallelStreams Class
45,000+ phrases
Search Phrases
Input Strings to Search
…
![Page 11: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/11.jpg)
11
• This example shows that the difference between sequential & parallel streams is often minuscule!
• Moreover, substantial speedups canoccur on multi-core processors!
• Superlinear speed-ups arise from“hyper-threaded” (virtual) cores
• Increases the # of independent instructions in the pipeline viaa superscalar architecture
Pros of the SearchWithParallelStreams Class
See en.wikipedia.org/wiki/Superscalar_processor
A superscalar processor can execute more than one instruction during a clock cycle by simultaneously dispatching multiple instructions to different execution units
![Page 12: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/12.jpg)
12
Cons of the SearchWithParallelStreams Class
![Page 13: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/13.jpg)
13
Starting SearchStreamGangTestPARALLEL_SPLITERATOR executed in 409 msecsCOMPLETABLE_FUTURES_INPUTS executed in 426 msecsCOMPLETABLE_FUTURES_PHASES executed in 427 msecsPARALLEL_STREAMS executed in 437 msecsPARALLEL_STREAM_PHASES executed in 440 msecsRXJAVA_PHASES executed in 485 msecsPARALLEL_STREAM_INPUTS executed in 802 msecsRXJAVA_INPUTS executed in 866 msecsSEQUENTIAL_LOOPS executed in 1638 msecsSEQUENTIAL_STREAM executed in 1958 msecsEnding SearchStreamGangTest
45,000+ phrases
Search Phrases
Input Strings to Search
…
• Just because two minuscule changes are needed doesn’t mean this is the best implementation!
Cons of the SearchWithParallelStreams Class
Other Java 8 concurrency/parallelism strategies are even more efficient..
Tests conducted on a 2.7GHz quad-core Lenovo P50 with 32 Gbytes of RAM
![Page 14: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/14.jpg)
14
• Just because two minuscule changes are needed doesn’t mean this is the best implementation!
Cons of the SearchWithParallelStreams Class
There’s no substitute for systematic benchmarking & experimentation
Starting SearchStreamGangTestPARALLEL_SPLITERATOR executed in 409 msecsCOMPLETABLE_FUTURES_INPUTS executed in 426 msecsCOMPLETABLE_FUTURES_PHASES executed in 427 msecsPARALLEL_STREAMS executed in 437 msecsPARALLEL_STREAM_PHASES executed in 440 msecsRXJAVA_PHASES executed in 485 msecsPARALLEL_STREAM_INPUTS executed in 802 msecsRXJAVA_INPUTS executed in 866 msecsSEQUENTIAL_LOOPS executed in 1638 msecsSEQUENTIAL_STREAM executed in 1958 msecsEnding SearchStreamGangTest
45,000+ phrases
Search Phrases
Input Strings to Search
…
![Page 15: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/15.jpg)
15
• We’ll show how to overcome these consin an upcoming lesson that focuses onthe SearchWithParallelSpliterator class
Cons of the SearchWithParallelStreams Class
Starting SearchStreamGangTestPARALLEL_SPLITERATOR executed in 409 msecsCOMPLETABLE_FUTURES_INPUTS executed in 426 msecsCOMPLETABLE_FUTURES_PHASES executed in 427 msecsPARALLEL_STREAMS executed in 437 msecsPARALLEL_STREAM_PHASES executed in 440 msecsRXJAVA_PHASES executed in 485 msecsPARALLEL_STREAM_INPUTS executed in 802 msecsRXJAVA_INPUTS executed in 866 msecsSEQUENTIAL_LOOPS executed in 1638 msecsSEQUENTIAL_STREAM executed in 1958 msecsEnding SearchStreamGangTest
45,000+ phrases
Search Phrases
Input Strings to Search
…
![Page 16: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed](https://reader035.fdocuments.net/reader035/viewer/2022070711/5ec97df1b216631abf2785e7/html5/thumbnails/16.jpg)
16
End of Java 8SearchWithParallelStreams
Example (Part 2)