1

My question is regarding Streams for Java. The code below is used to filter data from a generated text file with data using Streams. There are 2 parts to the code below, both doing the exact same thing. The only difference is that the first part runs in parallel while the latter does not. My question is why is it that the portion running in parallel is slower than the one not running in parallel? Is it due to caching or am I missing out on something about Java Streams?

StopWatch stopWatch = new StopWatch();
stopWatch.start();
long countOfEvenCards = Files.lines(Paths.get(fileName)).map(line -> line.split(" ")).parallel()
        .filter(line -> !Arrays.asList(errorNumbers).contains(line[0]))
        .filter(line -> !Arrays.asList(errorSuits).contains(line[1]))
        .filter(line -> !Arrays.asList(new String[] {"J", "Q", "K"}).contains(line[0]))
        .filter(line -> {
            if (line[0].equals("A")) {
                return true;
            } else {
                int number = Integer.parseInt(line[0]);
                if (number % 2 == 0) {
                    return true;
                } else {
                    return false;
                }
            }
        }).count();
long elapsedTime = stopWatch.stop();

System.out.println("Count Of Even Cards: " + countOfEvenCards);
System.out.println("Elapsed Time: " + elapsedTime + "ms");

StopWatch stopWatch2 = new StopWatch();
stopWatch2.start();

countOfEvenCards = Files.lines(Paths.get(fileName)).map(line -> line.split(" "))
        .filter(line -> !Arrays.asList(errorNumbers).contains(line[0]))
        .filter(line -> !Arrays.asList(errorSuits).contains(line[1]))
        .filter(line -> !Arrays.asList(new String[] {"J", "Q", "K"}).contains(line[0]))
        .filter(line -> {
            if (line[0].equals("A")) {
                return true;
            } else {
                int number = Integer.parseInt(line[0]);
                if (number % 2 == 0) {
                    return true;
                } else {
                    return false;
                }
            }
        }).count();

elapsedTime = stopWatch2.stop();
System.out.println("Count Of Even Cards: " + countOfEvenCards);
System.out.println("Elapsed Time: " + elapsedTime + "ms");
AdamIJK
  • 615
  • 2
  • 12
  • 22
Stackr128
  • 11
  • 1
  • 4
    Ignoring the bad code and the incorrect performance testing, I'd say that it's mainly because you're trying to parallelize a job that doesn't parallelize well. Most of the time goes to IO, and all your threads do irrelevant low-power work. Multiple threads will only slow you down. Don't use `parallel()` unless you understand it. It's not a magic recipe for speed, as you might notice. – Kayaman Oct 26 '17 at 16:45

0 Answers0