So Here is an example: I have a table full of numbers:

foo = Table[Table[RandomComplex[], {i, 1000}], {j, 8192}];

Now I want to do things with some elements of this table:

Table[Abs[foo[[1, 21]] - foo[[1, -20]]], {249}];

And this runs fine and takes about 0 seconds to evaluate. But I want to do more:

Table[Abs[foo[[1, 21]] - foo[[1, -20]]], {250}];

And suddenly it takes about 3 seconds. Why whould that be when I just increased the . But I want to do even more:

Table[RandomComplex[]*Table[Abs[foo[[1, 21]] - foo[[1, -20]]], {250}], {249}];

And this still takes about 3 seconds. But now I want to take it one step further:

Table[RandomComplex[]*Table[Abs[foo[[1, 21]] - foo[[1, -20]]], {250}], {250}];

And this takes forever and I get a CPU load of 60% on my quadcore. As if something is being evaluated in parallel even though I did not ask for it. And it takes 1001 seconds to evaluate. How is this possible and why is there a magic number 250 involved? It there a way to solve this issue without chopping my data into chunks of size<250 before processing?

Does anyone have the same issue or can recreate this problem on their system? Is there a way to solve this?

  • 2
    $\begingroup$ Perhaps SystemOptions["CompileOptions" -> "TableCompileLength"]? $\endgroup$
    – Michael E2
    Commented Feb 11, 2021 at 14:55
  • $\begingroup$ This looks like the same MSE issue that was reported as a bug several days ago. $\endgroup$ Commented Jan 20 at 16:26

2 Answers 2


Perhaps this:

  "CompileOptions" -> "TableCompileLength" -> Infinity],
 Table[Abs[foo[[1, 21]] - foo[[1, -20]]], {2500}]; // AbsoluteTiming,
 SetSystemOptions["CompileOptions" -> "TableCompileLength" -> 250]

(*  {0.006308, Null}  *)

Pre V12.2, use WithCleanup = Internal`WithLocalSettings.

Alternatively, Compile your Table:

Compile[{{foo, _Complex, 2}},
      Table[Abs[foo[[1, 21]] - foo[[1, -20]]], {250}], {250}]
    ][foo]; // AbsoluteTiming

(*  {0.014384, Null}  *)
  • $\begingroup$ Perfect. This works just fine. Is it potentially unsafe to always set SetSystemOptions["CompileOptions" -> "TableCompileLength" -> Infinity] ? Also is there any documentation on this? $\endgroup$ Commented Feb 11, 2021 at 17:41
  • 1
    $\begingroup$ @WalterLarsLee It should be fairly safe. Often you want a medium-low setting because Compile normally speeds things up. Setting it to Infinity might slow down a lot of other things. I imagine (don't know for sure) that in your case foo is getting copied into the compiled code when the limit is low, and that takes time to copy and compile. It seems to be a one-time expense and lengthening the table does not lengthen the running time much. It might pay off to leave the limit low if you have a high number of iterations, since with the compiled function, each iteration might be faster. $\endgroup$
    – Michael E2
    Commented Feb 11, 2021 at 18:13

With any version we can use

foo = Table[Table[RandomComplex[], {i, 1000}], {j, 8192}];

   With[{s1 = {250}, s2 = {250}, 
     q = Abs[foo[[1, 21]] - foo[[1, -20]]]}, 
    Table[RandomComplex[]*Table[q, s1], s2]]; // AbsoluteTiming

Out[]= {0.0010452, Null}

In more general case it takes about {0.144313, Null}

f[i_, j_] := Abs[foo[[i, 21]] - foo[[j, -20]]]
With[{s1 = {i, 250}, s2 = {j, 250}}, 
   Table[RandomComplex[]*Table[f[i, j], s1], s2]]; // AbsoluteTiming
  • $\begingroup$ very interesting. But it seems to me that this only works because it explicitly shifts the calculation Abs[foo[[1, 21]] - foo[[1, -20]]] outside of the Table. eventually I would like to use an iterator such that q would become something like q[i_,j_] and that seems to break this solution. $\endgroup$ Commented Feb 11, 2021 at 17:37
  • 1
    $\begingroup$ @WalterLarsLee Could you define expression q[i,j] to check what actually break? $\endgroup$ Commented Feb 11, 2021 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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