java 8 searchwithparallelstreams example (part 2)schmidt/cs891f/2018-pdfs/... · applied in...

16
Java 8 SearchWithParallelStreams Example (Part 2) Douglas C. Schmidt [email protected] www.dre.vanderbilt.edu/~schmidt Professor of Computer Science Institute for Software Integrated Systems Vanderbilt University Nashville, Tennessee, USA

Upload: others

Post on 22-May-2020

14 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java 8 SearchWithParallelStreams Example (Part 2)schmidt/cs891f/2018-PDFs/... · applied in SearchWithParallelStreams ... RXJAVA_INPUTS executed in 866 msecs SEQUENTIAL_LOOPS executed

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

16

End of Java 8SearchWithParallelStreams

Example (Part 2)