Wednesday 19 September 2018

Sequential V/S Parallel Stream


Sequential Stream and Parallel Stream in Java

We should remember to create parallel stream by calling parallelStream() method on the collection. If we are not calling parallelStream method then by default the sequential stream gets returned by stream() method.



Question: - When we can use Parallel Stream?

A parallel stream has a much higher overhead compared to a sequential one. I would like to use sequential streams by default and only consider parallel stream when :
  • If there is huge amount of items to process 
  • If I have a performance issue with the sequential stream.
  • They should be used when the output of the operation is not needed to be dependent on the order of elements present in source collection

Parallel Stream has equal performance impacts. Since each sub-stream is a single thread running and acting on the data, it has overhead compared to the sequential 
stream.

Customer.java

package com.gaurav.stream;
/**
 * @author Kumar Gaurav 
 * 
 */
public class Customer {
private int custId;
private double shoppingExpenditure;
public Customer(int custId, double shoppingExpenditure) {
super();
this.custId = custId;
this.shoppingExpenditure = shoppingExpenditure;
}
public int getCustId() {
return custId;
}
public void setCustId(int custId) {
this.custId = custId;
}
public double getShoppingExpenditure() {
return shoppingExpenditure;
}
public void setShoppingExpenditure(double shoppingExpenditure) {
this.shoppingExpenditure = shoppingExpenditure;
}
}

SequentialParallelStreamExample.java

package com.gaurav.stream;

/** In this example we have created a list of 500 employees out of which there are 400 customers whose shopping expenditure is more than 35000. 
 * We will see the time taken to process this filtered list by sequential and parallel stream.*/

import java.util.ArrayList;
import java.util.List;
/**
 * @author Kumar Gaurav 
 * 
 */
public class SequentialParallelStreamExample {

public static void main(String[] args) {

List<Customer> customerList = new ArrayList<Customer>();
for (int i = 0; i < 50; i++) {
customerList.add(new Customer(101, 345000));
customerList.add(new Customer(110, 90000));
customerList.add(new Customer(201, 35002));
customerList.add(new Customer(1011, 6987));
customerList.add(new Customer(301, 45001));
customerList.add(new Customer(1035, 32000));
customerList.add(new Customer(11010, 37000));
customerList.add(new Customer(1990, 85000));
customerList.add(new Customer(1999, 89235));
customerList.add(new Customer(11310, 49000));
}

/***** Creation of a Sequential Stream *****/
long startTime = System.currentTimeMillis();
System.out.println("Count of filtered List of Customer elements by Sequential Stream = "
+ customerList.stream().filter(e -> e.getShoppingExpenditure() > 35000).count());

long endTime = System.currentTimeMillis();
System.out.println("Time Taken by Sequential Stream = " + (endTime - startTime)+"ms" + "\n");

/***** Creation of a Parallel Stream *****/
startTime = System.currentTimeMillis();
System.out.println("Count of filtered List of Customer elements by Parallel Stream  = "
+ customerList.parallelStream().filter(e -> e.getShoppingExpenditure() > 35000).count());

endTime = System.currentTimeMillis();
System.out.println("Time Taken by Parallel Stream = " + (endTime - startTime)+"ms");
}
}

Output

Count of filtered List of Customer elements by Sequential Stream = 400
Time Taken by Sequential Stream = 38ms

Count of filtered List of Customer elements by Parallel Stream  = 400
Time Taken by Parallel Stream = 3ms

No comments:

Post a Comment