Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this not faster using parallel collections?

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.

like image 983
Plankalkül Avatar asked May 26 '11 21:05

Plankalkül


People also ask

What is a parallel collection?

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.

What is parallel programming in Scala?

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.

What is par in Scala?

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.


1 Answers

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.

like image 179
axel22 Avatar answered Oct 14 '22 23:10

axel22