Differences between revisions 1 and 2
Revision 1 as of 2020-03-20 16:13:30
Size: 128
Comment:
Revision 2 as of 2020-03-20 17:32:42
Size: 974
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:


=== Cythonのコードをひとつのファイルで完結させる ===
Cythonを使って高速化したはいいけど、元々ひとつのコードだったものが複数のファイルになってしまうのは嫌だ。という場合の小技。

コードのファイル名を cython_test.pyx とする。
最後に
{{{#!python
if __name__ == '__main__':
    import cython_test
    cython_test.main()
}}}
と書いておけば、コンパイルの時は読まれず、実行した時はmain関数が走る。このコードひとつをcythonizeしてからpythonに投げればおk
{{{
$ cythonize -a -i cython_test.pyx
Compiling /gpfs/home/(カレントディレクトリ)/cython_test.pyx because it changed.
[1/1] Cythonizing /gpfs/(カレントディレクトリ)/cython_test.pyx
$ python cython_test.pyx
}}}

高速化の小技

Cython

簡単なコンパイル

Cythonのコードをひとつのファイルで完結させる

Cythonを使って高速化したはいいけど、元々ひとつのコードだったものが複数のファイルになってしまうのは嫌だ。という場合の小技。

コードのファイル名を cython_test.pyx とする。 最後に

   1 if __name__ == '__main__':
   2     import cython_test
   3     cython_test.main()   

と書いておけば、コンパイルの時は読まれず、実行した時はmain関数が走る。このコードひとつをcythonizeしてからpythonに投げればおk

$ cythonize -a -i cython_test.pyx
Compiling /gpfs/home/(カレントディレクトリ)/cython_test.pyx because it changed.
[1/1] Cythonizing /gpfs/(カレントディレクトリ)/cython_test.pyx
$ python cython_test.pyx

KAGRA/Subgroups/PEM/PythonMemoJP/speedup (last edited 2021-08-04 18:33:51 by tatsuki.washimi)