I'm doing some experiments with streams. More specific I would like to compare standard streams to parallel streams. But, I experience very slow response time using parallel compared to standard streams. This strange because I Expected the opposite. Here's the test code I wrote for the experiment. Any suggestions are welcome.
package streamsExamples;
import java.util.OptionalDouble;
import java.util.stream.*;
import java.util.*;
import static java.util.stream.IntStream.of;
public class ParallelSpeedTest {
private static OptionalDouble optionalDouble;
private final static long LIMIT = 100000000;
private static Random random = new Random();
private static ArrayList<Integer> list= new ArrayList<Integer>();
public static void main(String[] args) {
long begin, end;
for(long i = 0; i < LIMIT; i++){
list.add(random.nextInt());
}
begin = System.currentTimeMillis();
streamTest();
end = System.currentTimeMillis();
System.out.println("Stream: " +(end - begin));
begin = System.currentTimeMillis();
parallelStreamTest();
end = System.currentTimeMillis();
System.out.println("Parallel Stream: " +(end - begin));
}
public static void streamTest() {
optionalDouble = IntStream
.generate(new Random()::nextInt)
.limit(LIMIT)
.average();
}
public static void parallelStreamTest(){
optionalDouble = IntStream
.generate(new Random()::nextInt)
.parallel()
.limit(100000000)
.average();
}
}