LoginSignup
7
4

More than 5 years have passed since last update.

Mathematicaのコードが10倍速くなる機能 (Mathematica8以降)

Posted at

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

CompilationTargetC.png

7
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
4