I compared performance for a simple "find max" logic in a 10 million integer array. The simple for loop performs much (at least 10 times better) than lambda and parallel stream version.
Could someone help me in understanding this counter intuitive behavior? I've a Quad core Intel i5 processor Dell E5530 laptop with Windows 7 professional installed, with 1.8.0_60 64 bit JVM.
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Random;
public class MaxFind
{
private static final int CAPACITY = 10_000_000;
public static void main(String[] args) throws Exception
{
List<Integer> numbers = new ArrayList<Integer>(CAPACITY);
Random rand = new Random();
long start = System.currentTimeMillis();
for(int i=0; i<CAPACITY; i++)
numbers.add(rand.nextInt());
long end = System.currentTimeMillis();
System.out.println("size of numbers: " + numbers.size() + ", time taken to populate: " + (end-start) + " milliseconds");
start = System.currentTimeMillis();
int maxNum = Integer.MIN_VALUE;
//imperative approach
for(int i=0; i<numbers.size(); i++)
maxNum = Integer.max(numbers.get(i), maxNum);
end = System.currentTimeMillis();
System.out.println("Time taken to find the max value " + maxNum + " using normal for loop: " + (end-start) + " milliseconds.");
start = System.currentTimeMillis();
//lambda, parallel stream
Optional<Integer> max = numbers.parallelStream().reduce(Integer::max);
end = System.currentTimeMillis();
System.out.println("Time taken to find the max value " + max.get() + " using lambda with parallel stream 1: " + (end-start) + " milliseconds.");
start = System.currentTimeMillis();
maxNum = numbers.parallelStream().reduce(Integer.MIN_VALUE, (a, b) -> Integer.max(a, b));
end = System.currentTimeMillis();
System.out.println("Time taken to find the max value " + max.get() + " using lambda with parallel stream 2: " + (end-start) + " milliseconds.");
}
}