Top Banner
Java SearchWithParallelStreams Example: Evaluating Pros & Cons Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA
17

Java SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

Jul 10, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Java SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

Java SearchWithParallelStreams

Example: Evaluating Pros & Cons

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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

2

Learning Objectives in this Part of the Lesson• Know how Java 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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

3

Pros of the SearchWithParallelStreams Class

Page 4: Java SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

12

Cons of the SearchWithParallelStreams Class

Page 13: Java SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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 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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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 SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

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

See SearchStreamGang/src/main/java/livelessons/streamgangs/SearchWithParallelSpliterator.java

Page 16: Java SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

16

• 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

SearchWithParallelSpliterator is thus the most aggressively parallelism strategy!

Page 17: Java SearchWithParallelStreams Example: Evaluating Pros & Consschmidt/cs891f/2019-PDFs/6.3.4-Java-sear… · RXJAVA_INPUTS executed in 774 msecs SEQUENTIAL_LOOPS executed in 1485

17

End of Java SearchWithParallelStreams Example: Evaluating Pros & Cons