Why is mathematica so slow when tables reach a certain length
Perhaps this:
WithCleanup[
SetSystemOptions[
"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[
RandomComplex[]*
Table[Abs[foo[[1, 21]] - foo[[1, -20]]], {250}], {250}]
][foo]; // AbsoluteTiming
(* {0.014384, Null} *)
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