Given a large list of elements, is it possible to improve Select
by parallelizing?
An example: from a 10,000,000-element list of integers between 1 and 10, select all primes
rl = RandomInteger[10, {10^7}];
Select[rl, PrimeQ] // AbsoluteTiming // First
(* 4.18468 *)
rlp = Partition[rl, 4];
LaunchKernels[];
Union[ParallelMap[Select[#, PrimeQ] &, rlp]]] // AbsoluteTiming // First
(* 83.7938 *)
I demonstrated my first naive attempt.
- What causes the huge time increase? Passing huge lists between kernels?
- Is there a better way to do this? (One that works for lists of elements other than integers)
- A slight variant: if I generate the large list in the first place, is it faster to use
Reap
/Sow
? (Which I see also has some parallelization issues.)
Reap
andSow
in parallelized code is described here. $\endgroup$Select
can be directly parallelized usingParallelize[Select[rl, PrimeQ]]
. This takes 4.1 seconds on my machine. The serial version takes 3.0 seconds. $\endgroup$