Mathematica のプログラムの実行速度を上げるにはどのようにすればよいのでしょうか.
最新バージョンを使う
Mathematica の新しいバージョンでは,スピードが改善していることや,計算のスピードが著しくアップする新たな組込み関数が加わっていることがあります.
例として,自己相関の計算についてバージョン4とバージョン3を比較してみます.
![[Graphics:Images/index_gr_1.gif]](Images/index_gr_1.gif)
バージョン3では,この計算に150秒かかります.
![[Graphics:Images/index_gr_2.gif]](Images/index_gr_2.gif)
![[Graphics:Images/index_gr_3.gif]](Images/index_gr_3.gif)
これはバージョン4を使うと約5倍も速くなります.
![[Graphics:Images/index_gr_4.gif]](Images/index_gr_4.gif)
![[Graphics:Images/index_gr_5.gif]](Images/index_gr_5.gif)
バージョン4にはリスト相関の計算のための新しい組込み関数が加わっています.これを用いると,計算速度はバージョン3に比べて340倍以上も速くなります.
![[Graphics:Images/index_gr_6.gif]](Images/index_gr_6.gif)
![[Graphics:Images/index_gr_7.gif]](Images/index_gr_7.gif)
Mathematica 4ではその他,以下のようなもののスピードが改善されました.
トップに戻る
組込み関数を使う
組込みのMathematica 関数は,通常ユーザが書いた同じ機能のコードよりはるかに効率よく動作します.この理由のひとつに,組込み関数が用いているアルゴリズムが当社独自のもので,これは高度に最適化されており,C言語で実装されているということが挙げられます.実装したい関数がすでにMathematica に組み込まれているかどうかを調べてみることが,時間短縮に繋がります.また,Mathematica は何千もの数学関数だけでなく,Sortなど数多くのユーティリティ関数も備えていることを忘れないでください.
以下に例を示します.次の式は自己相関を巧みに計算します.
![[Graphics:Images/index_gr_24.gif]](Images/index_gr_24.gif)
上記の方法はこの計算としては最も簡潔なトップレベルのコードです.これは少なくとも次のものよりはるかに優れています.
![[Graphics:Images/index_gr_25.gif]](Images/index_gr_25.gif)
また,データ数が少ない場合はかなり速く計算されるようです.
![[Graphics:Images/index_gr_26.gif]](Images/index_gr_26.gif)
![[Graphics:Images/index_gr_27.gif]](Images/index_gr_27.gif)
![[Graphics:Images/index_gr_28.gif]](Images/index_gr_28.gif)
しかし,次のような大きなデータセットを試してみるとどのくらいの時間がかかるでしょうか.
![[Graphics:Images/index_gr_29.gif]](Images/index_gr_29.gif)
![[Graphics:Images/index_gr_30.gif]](Images/index_gr_30.gif)
![[Graphics:Images/index_gr_31.gif]](Images/index_gr_31.gif)
組込み関数ListCorrelateを使うと,大きなデータセットに必要な計算時間が約2200倍も削減されました.
![[Graphics:Images/index_gr_32.gif]](Images/index_gr_32.gif)
![[Graphics:Images/index_gr_33.gif]](Images/index_gr_33.gif)
トップに戻る
機械精度の数値を使う
Mathematica で数値計算を行う上で最も重要な注意事項のひとつに,浮動小数点数に対して使われる評価メカニズムが,整数・分数などの厳密な数値に使われるものとは本質的に異なるということが挙げられます.通常,浮動少数点演算は厳密計算より速いので,それぞれを適切に使い分ける必要があります.
次の例をご覧ください.
![[Graphics:Images/index_gr_34.gif]](Images/index_gr_34.gif)
![[Graphics:Images/index_gr_35.gif]](Images/index_gr_35.gif)
![[Graphics:Images/faster2_gr_1.gif]](Images/faster2_gr_1.gif)
![[Graphics:Images/index_gr_37.gif]](Images/index_gr_37.gif)
![[Graphics:Images/index_gr_38.gif]](Images/index_gr_38.gif)
Mathematica でこの行列の固有値の計算をすると,約2.36秒かかりました.実際,これはかなり速いといえます.ここで全体を浮動小数点に変えてみましょう.
![[Graphics:Images/index_gr_39.gif]](Images/index_gr_39.gif)
すると,固有値の計算はほぼ瞬時に終りました.
![[Graphics:Images/index_gr_40.gif]](Images/index_gr_40.gif)
![[Graphics:Images/index_gr_41.gif]](Images/index_gr_41.gif)
トップに戻る
手続き型プログラミングを避ける
C,Fortran,Matlabなどの,他の言語から移行したユーザは,手続き型構造が使いたくなるでしょう.しかし,ほとんどの場合,手続き型プログラミングは関数型プログラミングに比べてパフォーマンスが非常に劣化してしまいます.
![[Graphics:Images/index_gr_42.gif]](Images/index_gr_42.gif)
![[Graphics:Images/index_gr_43.gif]](Images/index_gr_43.gif)
![[Graphics:Images/index_gr_44.gif]](Images/index_gr_44.gif)
![[Graphics:Images/index_gr_45.gif]](Images/index_gr_45.gif)
トップに戻る
リストを個々の部分としてではなく,全体として操作する
Mathematica はリスト全体を一度に操作することも,新しいPackedArrayテクノロジーを使って内部的に圧縮された形でリストを操作することもできます.後者を使うと,リストの各要素についてひとつひとつステップを踏んでいく場合よりも,計算が格段に速くなります.また,Partitionなどのリストの操作のための関数も利用することができます.
スピードの増加は,リストのすべての要素の正弦を取るなどの単純な計算の場合でさえも顕著です.
![[Graphics:Images/index_gr_46.gif]](Images/index_gr_46.gif)
![[Graphics:Images/index_gr_47.gif]](Images/index_gr_47.gif)
![[Graphics:Images/index_gr_48.gif]](Images/index_gr_48.gif)
![[Graphics:Images/index_gr_49.gif]](Images/index_gr_49.gif)
より現実的な例として,x とy の値の2つのベクトルを{x, y }というペアに結合する場合を考えてみます.
![[Graphics:Images/index_gr_50.gif]](Images/index_gr_50.gif)
この場合,新しい表を作るのがごく一般的な方法でしょう.
![[Graphics:Images/index_gr_51.gif]](Images/index_gr_51.gif)
![[Graphics:Images/index_gr_52.gif]](Images/index_gr_52.gif)
![[Graphics:Images/index_gr_53.gif]](Images/index_gr_53.gif)
![[Graphics:Images/index_gr_54.gif]](Images/index_gr_54.gif)
リストを結合して転置し,分割しても同様の結果が得られます.こちらの方が格段に速く計算されます.
![[Graphics:Images/index_gr_55.gif]](Images/index_gr_55.gif)
![[Graphics:Images/index_gr_56.gif]](Images/index_gr_56.gif)
![[Graphics:Images/index_gr_57.gif]](Images/index_gr_57.gif)
![[Graphics:Images/index_gr_58.gif]](Images/index_gr_58.gif)
トップに戻る
Compileを使う
数値計算の多くでは,組込みのコンパイラを使うとよりよいパフォーマンスを簡単に得ることができます.これは多くの場合,Compile[args, ...]で関数をラップするだけなので,簡単です.
![[Graphics:Images/index_gr_59.gif]](Images/index_gr_59.gif)
![[Graphics:Images/index_gr_60.gif]](Images/index_gr_60.gif)
![[Graphics:Images/index_gr_61.gif]](Images/index_gr_61.gif)
![[Graphics:Images/index_gr_62.gif]](Images/index_gr_62.gif)
![[Graphics:Images/index_gr_63.gif]](Images/index_gr_63.gif)
![[Graphics:Images/index_gr_64.gif]](Images/index_gr_64.gif)
Compileについての詳細情報は,Mathematica ブックの関連セクションをご覧ください.
トップに戻る
Parallel Computing Toolkit を使う
大規模な計算を実行しなければならない場合,Parallel Computing Toolkit がお探しのソリューションかもしれません.
The Parallel Computing Toolkit を使うと,ネットワーク上の複数のコンピュータ,またはマルチプロセッサのコンピュータにアクセスできる場合に,並列計算が可能になります.これは多くの並列プログラミングプリミティブを実装し,アニメーション,プロット,行列操作などの並列操作実行用のハイレベルコマンドを有しています.また,並列でのモンテカルロシミュレーション,ビジュアル化,検索,最適化など,人気の新しいプログラミング手法をサポートしています.
トップに戻る
MathCode C++ を使う
MathCode C++ はMathematica 用のアプリケーションパッケージで,数値計算用の最適化されたC++コードを生成します.
機能と利点
- コンパイルされたコードがMathematica 内から簡単に呼び出せます.
- スタンドアロンのコード(Mathematica とは独立に実行できるコード)も生成できます.
- 計算によっては最大5000倍もパフォーマンスが向上します.
- C,C++,Fortran77コードの外部ライブラリがMathematica 内から簡単に接続・呼び出しできます.
- 行列の抽出部分に対して,拡張された機能が使えます.
MathCode
C++ についての詳細情報は,製品のページをご覧ください.
トップに戻る
|