Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Using math 9.0.1 on a MacBook Air, I see a sudden increase of Timing by a factor of thousands when I sum over 250 elements of a matrix rather than over 249. So for instance, this table contains sums from 1 to 249 and it takes 0.0002s

Clear[vec, time];
vec = Table[i, {i, 100}, {j, 100}, {k, 300}];
time = Timing[
Table[Sum[vec[[i, j, k]], {k, 1, 249}], {i, 1}, {j, 1}]][[1]]; time

while if I go from 1 to 250

Clear[vec, time];
vec = Table[i, {i, 100}, {j, 100}, {k, 300}];
time = Timing[
Table[Sum[vec[[i, j, k]], {k, 1, 250}], {i, 1}, {j, 1}]][[1]]; time

it takes 1.4s. The huge increase occurs regardless of the content of vec, regardless of the upper limits of i,j, and it does not depend on whether I go from 1 to 250 or from 2 to 251, provided it's at least 250 entries. So if I sum from 2 to 250, it's back to 0.0002s. It depends instead on the size of vec (that is why I create a much larger matrix vec than actually needed in the sum). Can anybody reproduce this behavior? Any suggestion?

share|improve this question
This seems closely related. And setting the option @Karsten mentions does work. This looks like something to report to support. –  acl Jul 18 '14 at 16:35
@Karsten thanks, it works! A search of SumCompileLength on the Help->Documentation center returns no results, and CompileOptions returns useless (for me) information. –  user16397 Jul 18 '14 at 16:39
(note that the example I give in my answer to the other question doesn't demonstrate what you are seeing, but the solution is indeed to increase the option mentioned) –  acl Jul 18 '14 at 16:39
try "CompileOptions" /. SystemOptions[] or look at the way they're displayed in my answer to the other question –  acl Jul 18 '14 at 16:40
I can't believe that such a common operation like summing over more than 250 entries require tweaking the SetSystemOptions, which the manual recommend not to modify: –  user16397 Jul 18 '14 at 16:41

2 Answers 2

up vote 9 down vote accepted

The default SumCompileLength is 250.
You can increase this number for example to 500 using:

SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 500}]
share|improve this answer

If you don't want to change the system options just to make Sum auto-compile, then you could instead replace Sum by Total:

Clear[vec, time];
vec = Table[i, {i, 100}, {j, 100}, {k, 300}];
time = Timing[
   Table[Total[vec[[i, j, 1 ;; 250]]], {i, 1}, {j, 1}]][[1]]; time

The resulting timing doesn't show any significant difference between 249 and 250, and is just as fast as your first example.

share|improve this answer
This is almost always a good idea if what you want to do is a numerical sum. –  acl Jul 18 '14 at 20:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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