# Speeding up a for loop by removing a Sum command

I am attempting to create an array that has of order 1000 x 1000 entries. In the entry of the last column of each row, I have to carry out a sum that could contains 100s of terms (I only used 40 here so it would run in a reasonable time) :

AbsoluteTiming[
N[ParallelTable[
Table[
RandomReal[n]* KroneckerDelta[n, m] - RandomReal[n + m] + KroneckerDelta[n, 1000]*
Sum[Sech[(l - n - 1/2) \[Pi]/2], {l, 1000, 1000 + 40}],
{n, 1, 1000}],
{m, 1, 1000}], 12];]
{60.211444, Null}


Mathematica creates the array much more quickly when the Sum command is removed

AbsoluteTiming[
N[ParallelTable[
Table[
RandomReal[n]* KroneckerDelta[n, m] - RandomReal[n + m],
{n, 1, 1000}],
{m, 1, 1000}], 12];]
{1.125064, Null}


This isn't too surprising because Mathematica now doesn't have to do 1000 Sums. However, I was surprised at the level of the slow down. Is there a faster way to implement this Table using a workaround for the Sum?

I tried using Table and Total but there is no appreciable time difference for me. I also tried writing my own 'For' loops to both make the Table and compute the Sum but my implementation was yet slower than what I already had.

EDIT1: As per the comments I have changed my code so that the sum is evaluated symbolically once, and then simply evaluated when each row of the array is constructed. The array now builds in between half and a third of the time it took before. However, it is still a few dozens of times slower than the array without the Sum so I am still interested in how I can improve the performance further.

Am I just being unreasonable, expecting to be able to implement a sum like this for a small performance decline? It seems so simple and yet the performance hit is large.

-
I noticed that the sum doesn't include m so you can do the table without the sum, calculate just one sum and add it to the last column. Right now it seem like it does the same sum a 1000 times, for each row. –  Spawn1701D Apr 23 at 19:13
The sum is not exactly the same, because it depends on n (keep in mind also that this is a simplified example, my actual code has more complicated expressions). In my actual code, it does depend on m, but because all these sum entries appear in the rightmost column, m is the same for all of them. I think it might be worthwhile to calculate the sum once symbolically and then just substitute for each row. Certainly possible. I had assumed that doing the sum numerically is faster than symbolically, but probably not by 1000 times. –  Kevin Driscoll Apr 23 at 19:22
You have to understand that now it does a 1000 times the sum and the calculation of the sum. If you have the sum ready then you have only a 1000 evaluations of the sum. Mathematica is a symbolic platform so you have to take advantage of this fact as much as possible. –  Spawn1701D Apr 23 at 19:57
More to the point, what is span doing in the sum? Sum[Sech[(l - n - 1/2) [Pi]/2], {l, span, span + 40}] ... span is not defined, and so it is just cancelling out across the Tables. Changing the calculation from span + 40 to span + 10 ... doesn't change the output ... use a SeedRandom to check. –  wolfies Apr 23 at 20:44
Sorry about that! Span is a variable that occurs in my code, but which I had intended on omitting here for simplicity. Of course since it isn't defined you don't get something that makes sense. I'll edit to fix it. –  Kevin Driscoll Apr 23 at 22:02
show 1 more comment