Update
Following a comment of Szabolcs have changed Timing to AbsoluteTiming. The results are virtually the same.
Original post
I have accidentally observed the following surprising result. So, why should I use ParallelTable?
$Version
(* Out[114]= "10.1.0 for Microsoft Windows (64-bit) (March 24, 2015)" *)
p[x_] = x^2;
Measurement with Timing[]
Timing[Table[p[i], {i, 1, 10^5}]][[1]]
(* Out[118]= 0.156001 *)
Timing[ParallelTable[p[i], {i, 1, 10^5}]][[1]]
(* Out[119]= 0.530403 *)
some statistics
t0 := Timing[Table[p[i], {i, 1, 10^5}]][[1]]
tp := Timing[ParallelTable[p[i], {i, 1, 10^5}]][[1]]
rt := tp/t0
tt = Table[rt, {50}];
ms = {Mean[tt], StandardDeviation[tt]}
(* Out[72]= {5.79498, 0.898244} *)
Measurement with AbsoluteTiming[]
AbsoluteTiming[Table[p[i], {i, 1, 10^5}]][[1]]
(* Out[62]= 0.133115 *)
In[63]:= AbsoluteTiming[ParallelTable[p[i], {i, 1, 10^5}]][[1]]
(* Out[63]= 0.903214 *)
some statistics
t0a := AbsoluteTiming[Table[p[i], {i, 1, 10^5}]][[1]]
tpa := AbsoluteTiming[ParallelTable[p[i], {i, 1, 10^5}]][[1]]
rta := tpa/t0a
tta = Table[rta, {50}];
msa = {Mean[tta], StandardDeviation[tta]}
(* Out[68]= {5.94756, 0.933185} *)
Comparison
msa/ms
(* Out[94]= {1.0045, 1.10433} *)
The results for Timing[] and AbsoluteTiming[] are approximately the same.
EDIT: cases of advantageous parallelization
Let us define a function which measures the advantange of taking the parallel function over the normal one, i.e. the "normal" execution time divided by the parallel execution time:
advantageParallel[f_] :=
AbsoluteTiming[Table[f[n], {n, 1, 10^6}]][[1]]/
AbsoluteTiming[ParallelTable[f[n], {n, 1, 10^6}]][[1]]
and test some functions
advantageParallel /@ {EulerPhi, DivisorSigma[1, #] &, PrimeQ,FactorInteger,
MoebiusMu, #^2 &, Sin, Tanh, EllipticK[#/(1 + #) &]}
(* Out[60]= {1.46032, 1.30282, 0.153376, 0.213928,
0.667186, 0.162014, 0.108339, 0.104784, 0.0136566} *)
Hence for EulerPhi[]
(suggested by Quantum_Oli in a comment) and DivisorSigma[]
parallel execution is advantegous, for the rest of our selection the converse is true.
p
which takes more time to execute you should find a situation whereParallelTable
wins. $\endgroup$EulerPhi
with a table going up to10^6
elements. On my system it takes 3.7s usingTable
and 1.1s usingParallelTable
. $\endgroup$Timing
andAbsoluteTiming
for an example where there is some speed gain by parallelizing. The two will only give the approximately same results in cases where the overhead dominates and there is no speed gain (or more precisely when the time spent in the main kernel with handling overhead is comparable to the total computation time)... $\endgroup$