The default `SumCompileLength`

is 250.

You can increase this number for example to 500 using

```
SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 500}]
```

or to infinity using

```
SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> ∞}]
```

## What is `"SumCompileLength"`

for?

For sums with a finite number of at least `"SumCompileLength"`

elements autocompilation will be used to compute the sum.

## Visualization and explanation

For a `Sum`

with very simple summands, `Sum[k, {k, 1, n}]`

, the timings as a function of the number of elements `n`

using the default settings

```
SystemOptions["CompileOptions" -> "SumCompileLength"]
```

$\ $`{"CompileOptions" -> {"SumCompileLength" -> 250}}`

can be visualized with

```
defaultTimings = First@AbsoluteTiming[Sum[k, {k, 1, #}]] &~Array~500;
ListPlot[defaultTimings, PlotRange -> All, Joined -> True,
PlotLegends -> "defaultTimings: \"SumCompileLength\"\[Rule]250"]
```

As described by the OP there is a huge jump in the timings at 250 elements. This is due to the fact that the time needed to perform the autocompilation is longer than the time saved by using the autocompiled version. Additionally one can observe that the slope is less steep for more than 250 elements, because, after the autocompilation is done, using the autocompiled version is actually faster than using the non-autocompiled version.

## When `"SumCompileLength"`

should not be increased

For the very simple summand given in the question and for 250 and some more elements increasing `"SumCompileLength"`

as shown in the beginning of this answer reduces the time needed to compute the `Sum`

. However, it would be wrong to conclude that `"SumCompileLength"`

should always be increased or set to infinity.

**1) Using the **`Sum`

multiple times

```
do1 = (SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 250}];
First@AbsoluteTiming[RandomReal[]*Sum[k, {k, 1, #}]] &~Array~500);
do100Default = (SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 250}];
First@AbsoluteTiming[Do[RandomReal[]*Sum[k, {k, 1, #}], {100}]]/100. &~Array~500);
do100SCL∞ = (SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> ∞}];
First@AbsoluteTiming[Do[RandomReal[]*Sum[k, {k, 1, #}], {100}]]/100. &~Array~500);
do100SCL1 = (SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 1}];
First@AbsoluteTiming[Do[RandomReal[]*Sum[k, {k, 1, #}], {100}]]/100. &~Array~500);
ListPlot[{do1, do100Default, do100SCL∞, do100SCL1}, PlotRange -> All, Joined -> True,
PlotStyle -> Thick, PlotLegends -> {"do1", "do100Default", "do100SCL∞]", "do100SCL1"}]
```

In situations where the autocompiled version of the `Sum`

can be reused, it is advantageous to reduce `"SumCompileLength"`

.

**2) **`Sum`

over a huge number of elements

```
scl250 = (SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 250}];
First@AbsoluteTiming[Sum[k, {k, 1, #}]] &~Array~1000);
scl1 = (SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 1}];
First@AbsoluteTiming[Sum[k, {k, 1, #}]] &~Array~1000);
scl∞ = (SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> ∞}];
First@AbsoluteTiming[Sum[k, {k, 1, #}]] &~Array~1000);
ListPlot[{scl250, scl1, scl∞}, PlotRange -> All, Joined -> True,
PlotStyle -> Thick, PlotLegends -> {"\"SumCompileLength\" \[Rule] 250",
"\"SumCompileLength\" \[Rule] 1", "\"SumCompileLength\" \[Rule] ∞"},
Epilog -> {Red, Line[{{550, 0}, {550, 1}}]}]
```

For this example using autocompilation is already beneficial for more than approx. 550 elements.

**3) Computational expensive, compilable summands**

For example `LogGamma`

is a compilable function that is computational more expensive than the previous example.

```
scl250 = (SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 250}];
First@AbsoluteTiming[Sum[N@LogGamma[k], {k, 1, #}]] &~Array~350);
scl1 = (SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> 1}];
First@AbsoluteTiming[Sum[N@LogGamma[k], {k, 1, #}]] &~Array~350);
scl∞ = (SetSystemOptions["CompileOptions" -> {"SumCompileLength" -> ∞}];
First@AbsoluteTiming[Sum[N@LogGamma[k], {k, 1, #}]] &~Array~350);
ListPlot[{scl250, scl1, scl∞}, PlotRange -> All, Joined -> True,
PlotStyle -> Thick, PlotLegends -> {"\"SumCompileLength\" \[Rule] 250",
"\"SumCompileLength\" \[Rule] 1", "\"SumCompileLength\" \[Rule] ∞"},
Epilog -> {Red, Line[{{50, 0}, {50, 1}}]}]
```

Here the autocompiled version already starts to outperform the non-autocompiled version at about 50 elements.

1This seems closely related. And setting the option @Karsten mentions does work. This looks like something to report to support. – acl – 2014-07-18T16:35:27.320

@Karsten thanks, it works! A search of SumCompileLength on the Help->Documentation center returns no results, and CompileOptions returns useless (for me) information. – user16397 – 2014-07-18T16:39:22.643

(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 – 2014-07-18T16:39:38.610

try

`"CompileOptions" /. SystemOptions[]`

or look at the way they're displayed in my answer to the other question – acl – 2014-07-18T16:40:56.873I 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 – 2014-07-18T16:41:24.317

It should not :) but it seems to do that in v9 as well – acl – 2014-07-18T16:42:20.323

@acl thanks! I see that these SystemOptions can be really crucial at times. Need to remember that! – user16397 – 2014-07-18T16:44:04.487