# How can I understand this short example?

First launch kernels

LaunchKernels[4]

{KernelObject[5, "local"], KernelObject[6, "local"], KernelObject[7, "local"], KernelObject[8, "local"]}


Then I calculate This cell

f1 = Table[ParallelSubmit[{j}, Sum[N@Gamma[i + j], {i, 10000}]], {j, 4}];
out1=AbsoluteTiming[WaitAll[f1]];out1[[1]]


The output is

4.025230


But when I just set a=0

a = 0;
f2 = Table[ParallelSubmit[{j}, Sum[N@Gamma[a + i + j], {i, 10000}]], {j, 4}];
out2 = AbsoluteTiming[WaitAll[f2]]; out2[[1]]


The output

0.729042


Is much speeder then before.

The result is same

out1[[2]] == out2[[2]]
True


Why? I'm very confused.

The definition a = 0 is not being distributed among the subkernels, therefore in f2 the Sum is evaluated symbolically. After the results are returned to the master kernel a is substituted in.

It happens that in this case a symbolic sum is much faster:

ClearAll[a]

Sum[N@Gamma[i + 1], {i, 10000}] // AbsoluteTiming

Sum[N@Gamma[a + i + 1], {i, 10000}] /. a -> 0 // AbsoluteTiming

{1.08047, 2.846544335353437*10^35659}

{0.0687247, 2.8465443354*10^35659}


The reason is that in the first line Gamma is being computed with exact arithmetic, then converted to machine precision with N. If we simply use 1 instead it is fast:

Sum[Gamma[i + 1], {i, 10000}] // AbsoluteTiming

{0.0585066, 2.8465443354*10^35659}


The second case is fast because Gamma is does not have NHoldAll or similar, therefore 1 is converted to 1. which has the same effect:

N @ Gamma[a + i + 1]

Gamma[1. + a + i]


Reference: Converting to machine precision

• Beat me to it... (+1)
– kale
Jun 24, 2015 at 2:25
• @kale Sorry, but thank you. Jun 24, 2015 at 2:26