#whats more efficient?
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
It is generally more efficient to use flatMap to create one big stream instead of creating multiple smaller streams in a loop. This is because creating and combining multiple streams can be more resource-intensive compared to just creating one large stream. FlatMap also helps in maintaining code readability and simplicity.
Useful links:
im not sure from what number of items parallel stream is better than a regular stream
Uh wut?
I don’t see how that answers the question
iterable?
i wanted to use streams
which i think using parallel is just faster than a loop anyways
but its also easier to work with for this
though if a loop is much faster ill switch to that
jmh?
one big stream
I've done streams over a billion elements (those I did parallel), and it works just fine, and it was pretty fast as well
microbenchmarking library for java
but not sure if there is a need here
these numbers are still pretty small
60k is small?
ah
Well depends what kind of computations you are doing but generally yes
oh so i shouldnt be using a parallel stream?
Well, it depends, do you even see a difference?
Parallel stream is probably significantly better above 400k-ish mark
my parallelstream read multiple files in parallel while parsing them
So that's why I did it
public void process(Consumer<Stream<String>> consumer) {
int processors = Runtime.getRuntime().availableProcessors() / 4;
ForkJoinPool forkJoinPool = new ForkJoinPool(processors);
try {
forkJoinPool.submit(() -> consumer
.accept(sourceFiles.stream()
.parallel()
.flatMap(FileProcessor::readFile)
)
).get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException(e);
} finally {
forkJoinPool.shutdown();
}
}
I also needed to limit the amount of cpus etc