I recently tried to use ParallelMap
instead of Map
and to my surprise encountered that ParallelMap
seems to be slower in general than Map
, which does not make sense to me.
Here is a simple test case, that shows the behavior on my system (tested it on Linux 64 Bit QuadCore i7 and on MacOS DualCore Core2Duo, both running Mathematica 9.0.1):
LaunchKernels[];
f[x_] := Sin[x] + Cos[x] + Tan[x];
test = Table[i, {i, 100000}];
Timing results are the following (and they are consistent, additional kernels have been started before):
Map[f[#] &, test]; // AbsoluteTiming
{0.198230, Null}
ParallelMap[f[#] &, test]; // AbsoluteTiming
{0.650516, Null}
What am I missing here?
ParallelXYZ
works best for slow functions. Often you can save time by faster functional implementations (examples galore around here), consider e.g.:Sin[#] + Cos[#] + Tan[#] &[Range[100000]]
$\endgroup$ParallelMap
is a bit faster whentest
is replaced bytest = N @ Range[1000000]
and gets faster as the size grows. Approximate real versus exact results makes a 10x difference in the amount of data transferred back to the main kernel. That is surely a factor in the timings. $\endgroup$