2
$\begingroup$

Three methods("Sum", "Total@Table" and "Do") have been used to do the same work. The "Sum" and "Total@Table" function becomes extremely slow at num=250 (This number may depend on Computer's condition. My Laptop is MacBook Pro 2013 Late with 8G DRAM). I hope to understand the reason and how to AVOID it

timelist = ConstantArray[0, {3, 20}];
Do[
 num = nn*1 + 239;
 mat = RandomReal[{0, 1}, {num, num, 2, 2, 2}];
 timelist[[1, nn]] = 
  Timing[r1 = Sum[mat[[l, 1]] l, {l, 1, num}];][[1]];
 timelist[[2, nn]] = 
  Timing[r2 = Total@Table[mat[[l, 1]] l, {l, 1, num}];][[1]];
 timelist[[3, nn]] = Timing[r3 = ConstantArray[0, Dimensions[r2]];
    Do[r3 = r3 + mat[[l, 1]] l, {l, 1, num}];][[1]];
 (*SameQ[r1,r2,r3]*)
 , {nn, 1, 20}]
ListLinePlot[timelist, DataRange -> {240, 260}, 
 PlotLegends -> {"Sum", "Total@Table", "Do"}, 
 AxesLabel -> {"num", "Seconds"}, ScalingFunctions -> "Log"]

Timing Results(My laptop is MacBook 13 2013)

$\endgroup$
1

1 Answer 1

5
$\begingroup$

An FYI, too long for a comment, concerning a 4th approach taking advantage of vectorization in the MKL. (The answer to the main question, which is connected to system Compile thresholds, may be found at Sudden increase in timing when summing over 250 entries, which was pointed out by @kglr.)

timelist = ConstantArray[0, {4, 20}];
Do[num = nn*1 + 239;
 mat = RandomReal[{0, 1}, {num, num, 2, 2, 2}];
 timelist[[1, nn]] = 
  AbsoluteTiming[r1 = Sum[mat[[l, 1]] l, {l, 1, num}];][[1]];
 timelist[[2, nn]] = 
  AbsoluteTiming[r2 = Total@Table[mat[[l, 1]] l, {l, 1, num}];][[1]];
 timelist[[3, nn]] = 
  AbsoluteTiming[r3 = ConstantArray[0, Dimensions[r2]];
    Do[r3 = r3 + mat[[l, 1]] l, {l, 1, num}];][[1]];
 timelist[[4, nn]] = 
  AbsoluteTiming[r4 =  Total[mat[[All, 1]] Range[num]]; ][[1]],
 {nn, 1, 20}]
ListLinePlot[timelist, DataRange -> {240, 260}, 
 PlotLegends -> {"Sum", "Total@Table", "Do", "Total@vectorized"}, 
 AxesLabel -> {"num", "Seconds"}, ScalingFunctions -> "Log"]

enter image description here

r1 == r2 == r3 == r4
(*  True  *)

Concerning my preference for AbsoluteTiming: Difference between AbsoluteTiming and Timing

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.