https://www.330k.info/essay/how-to-optimize-mathematica-programs/
にも記載されているがCompileのオプション CompilationTarget -> "C" を使うといい。コードが10倍も速くなった!!
例
DoubleCircles = Compile[{{d, _Real}, {th _Real}, {r, _Real}},
Table[
If[
((i - 1000. - d Cos[th])^2 + (j - 1000. +
d Sin[th])^2 <
r^2) || ((i - 1000. + d Cos[th])^2 + (j - 1000. -
d Sin[th])^2 < r^2), 1., 0.
], {i, 2000}, {j, 2000}],
CompilationTarget -> "C",
RuntimeOptions -> "Speed"
];
DoubleCircles2 = Compile[{{d, _Real}, {th, _Real}, {r, _Real}},
Table[
If[
((i - 1000. - d Cos[th])^2 + (j - 1000. +
d Sin[th])^2 <
r^2) || ((i - 1000. + d Cos[th])^2 + (j - 1000. -
d Sin[th])^2 < r^2), 1., 0.
], {i, 2000}, {j, 2000}],
RuntimeOptions -> "Speed"
];
ArrayPlot[DoubleCircles[500, -45 Degree, 100]] // AbsoluteTiming
ArrayPlot[DoubleCircles2[500, -45 Degree, 100]] // AbsoluteTiming