# Why the "Sum" function becomes extremely slow at a specific size of matrix? How to AVOID it? [duplicate]

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"]


• – kglr
Commented Dec 3, 2020 at 15:59

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"]


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


Concerning my preference for AbsoluteTiming: Difference between AbsoluteTiming and Timing