I just wanted to test the parallel collections a bit and I used the following line of code (in REPL):
(1 to 100000).par.filter(BigInt(_).isProbablePrime(100))
against:
(1 to 100000).filter(BigInt(_).isProbablePrime(100))
But the parallel version is not faster. In fact it even feels a bit slower (But I haven't really measured that).
Has anyone an explanation for that?
Edit 1: Yes, I do have a multi-core processor
Edit 2: OK, I "solved" the problem myself. The implementation of isProbablePrime
seems to be the problem and not the parallel collections. I replaced isProbablePrime
with another function to test for primality and now I get an expected speedup.
Creating a Parallel CollectionAn example is List – it's converted into a standard immutable parallel sequence, which is a ParVector . Of course, the copying required for these collection types introduces an overhead not incurred by any other collection types, like Array , Vector , HashMap , etc.
Parallel computing is a type of computation where different computations can be performed at the same time. Basic principle: The problem can be divided into subproblems, each of which can be solved simultaneously. Many of us cannot find how parallel programming differs from concurrent programming.
The par function is short for parallel, and represents a convenient way of accessing Scala's Parallel Collections. These are designed from the ground up to work seamlessly with both Scala's Mutable and Immutable collection data structures.
Both with sequential and parallel ranges, filter
will generate a vector data structure - a Vector
or a ParVector
, respectively.
This is a known problem with parallel vectors that get generated from range collections - transformer methods (such as filter
) for parallel vectors do not construct the vector in parallel.
A solution for this that allows efficient parallel construction of vectors has already been developed, but was not yet implemented. I suggest you file a ticket, so that it can be fixed for the next release.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With