とらりもんHOME  Index  Search  Changes  Login

cython

2017/01/03

Cythonとは

まずはチュートリアル基礎編を読む。

次に言語仕様を眺めてNumpyのページ を眺める。

より詳しく知りたい人はドキュメントを読む。

基本

端末上で使う場合

  • hoge.pyxでPythonプログラムを書く。
  • setup.pyを作成
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("hoge", ["hoge.pyx"])]
)
  • Cythonをビルド。
$ python setup.py build_ext --inplace

Jupyter notebookで使う場合

一行目に

%load_ext Cython

使用したいコードの一行目に

%%cython 

言語仕様(メモ)

公式の言語仕様

複数定義

cdef: でインデントする。

cdef:
   int i
   float f

関数定義

def (Python)と cdef (Cの関数)がある。

Cythonモジュールの外側にエクスポートする場合はcdefは使えない。

Last modified:2020/02/12 08:21:33
Keyword(s):
References: