I was just evaluating, which of the code snippets performs better in java 8.
Snippet 1 (Processing in the main thread):
public long doSequence() {
DoubleStream ds = IntStream.range(0, 100000).asDoubleStream();
long startTime = System.currentTimeMillis();
final AtomicLong al = new AtomicLong();
ds.forEach((num) -> {
long n1 = new Double (Math.pow(num, 3)).longValue();
long n2 = new Double (Math.pow(num, 2)).longValue();
al.addAndGet(n1 + n2);
});
System.out.println("Sequence");
System.out.println(al.get());
long endTime = System.currentTimeMillis();
return (endTime - startTime);
}
Snippet 2 (Processing in parallel threads):
public long doParallel() {
long startTime = System.currentTimeMillis();
final AtomicLong al = new AtomicLong();
DoubleStream ds = IntStream.range(0, 100000).asDoubleStream();
ds.parallel().forEach((num) -> {
long n1 = new Double (Math.pow(num, 3)).longValue();
long n2 = new Double (Math.pow(num, 2)).longValue();
al.addAndGet(n1 + n2);
});
System.out.println("Parallel");
System.out.println(al.get());
long endTime = System.currentTimeMillis();
return (endTime - startTime);
}
Snippet 3 (Processing in parallel threads from a thread pool):
public long doThreadPoolParallel() throws InterruptedException, ExecutionException {
ForkJoinPool customThreadPool = new ForkJoinPool(4);
DoubleStream ds = IntStream.range(0, 100000).asDoubleStream();
long startTime = System.currentTimeMillis();
final AtomicLong al = new AtomicLong();
customThreadPool.submit(() -> ds.parallel().forEach((num) -> {
long n1 = new Double (Math.pow(num, 3)).longValue();
long n2 = new Double (Math.pow(num, 2)).longValue();
al.addAndGet(n1 + n2);
})).get();
System.out.println("Thread Pool");
System.out.println(al.get());
long endTime = System.currentTimeMillis();
return (endTime - startTime);
}
Output is here:
Parallel
6553089257123798384
34 <--34 milli seconds
Thread Pool
6553089257123798384
23 <--23 milli seconds
Sequence
6553089257123798384
12 <--12 milli seconds!
What I expected was
1) Time for processing using thread pool should be minimum, but its not true.(Note that i have not included the thread pool creation time, so it should be fast)
2) Never expected code running in sequence to be the fastest, what should be the reason for that.
I am using a quad core processor.
Appreciate any help to explain the above ambiguity!