少しのcython





自己隔離のおかげで、私たちはCythonaを手に入れました。問題は散発的です-構文の損失を最小限に抑えてpythonで加速する方法。1つのアプローチは、Cython(Cとpythonの混合)を使用することです。大きなタイトル



出版物幽霊が出るしかし、公式と結果の表が正しくないため、出版物の内容からはほとんど学ぶことができません。投稿の作成者が始めた写真を完成させて、とにドットを付けてみましょう。



*テストはodroidxu4、ubuntu mate、python2.7.17で実行されました。

Cythonは簡単にインストールできます(pip install cython)。



私たちはすべて同じフィボナッチ番号を拷問します。パフォーマンス向上テスト用のファイルを作成しましょう。python言語(test.py)の場合:



def test(n):
   a, b = 0.0, 1.0
   for i in range(n):
      a, b = a + b, a
   print (a)


cython言語(test2.pyx)の場合:



def test2(int n):
   cdef int i
   cdef double a=0.0, b=1.0
   for i in range(n):
      a, b = a + b, a
   print (a)


cythonファイルには事前ビルドが必要です。次の内容でsetup.pyを作成しましょう。



from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize('test2.pyx'))


そして収集する:



python setup.py build_ext --inplace


次に、前述の投稿からテストを使用してファイルを取得し、開始時に独自の番号を入力する機能(tests.py)を追加して、ファイルを少し修正しましょう。



import test
import test2
import time

number = input('enter number: ')

start = time.time()
test.test(number)
end =  time.time()

py_time = end - start
print("Python time = {}".format(py_time))

start = time.time()
test2.test(number)
end =  time.time()

cy_time = end - start
print("Cython time = {}".format(cy_time))
print("Speedup = {}".format(py_time / cy_time))


何が起きたのか見てみましょう:



python tests.py


結果:



python 2.7の場合:

enter number: 10
Python time = 1.62124633789e-05
Cython time = 4.05311584473e-06
Speedup = 4.0

enter number: 100
Python time = 3.40938568115e-05
Cython time = 5.00679016113e-06
Speedup = 6.80952380952

enter number: 1000
Python time = 0.000224113464355
Cython time = 1.19209289551e-05
Speedup = 18.8

enter number: 100000
Python time = 0.0200171470642
Cython time = 0.000855922698975
Speedup = 23.3866295265




python 3の場合:



enter number: 10
Python time = 7.653236389160156e-05
Cython time = 2.8133392333984375e-05
Speedup = 2.7203389830508473

enter number: 100
Python time = 8.678436279296875e-05
Cython time = 3.170967102050781e-05
Speedup = 2.736842105263158

enter number: 1000
Python time = 0.00031876564025878906
Cython time = 4.673004150390625e-05
Speedup = 6.821428571428571

enter number: 100000
Python time = 0.01643967628479004
Cython time = 0.0004260540008544922
Speedup = 38.5858981533296




* test2.pyxモジュールは、次のコマンドで「再構築」 されました。python3setup.py

build_ext --inplace

** cythonによってインストールされました:

pip3 install cython



setup.pyを使用してtest2.pyxをビルドせずに実行できます。このため、tests.pyファイルに行を追加するだけです。 :



import pyximport
pyximport.install()


これで、tests.pyが実行されるたびにtest2.pyxがオンザフライでビルドされ、フォルダー内のファイルが少なくなります。



Windowsでcythonを開始する方法。



cythonではpython3とpython2の両方のファイルをアセンブルできるという事実にもかかわらず、python3の既製のレシピ取得することはできませんでした

python3では、ビルドコマンドは機能します。

python setup.py build_ext -i --compiler=msvc


ただし、本格的な作業を行うには、visual studio 2019コンポーネントの一部をインストールする必要があります。正確にインストールするものは、ここのソリューションに示されています



したがって、cythonを使用してWindowsで作業(ファイルを作成)できるようにする2つの作業オプションがあります。



最初のものはpython2.7とmingwコンパイラを使用します。

手順は以下のとおりです。

1.python2.7の下にcython自体をインストールします。

py -2 -m pip install cython


2.mingwコンパイラをインストールします

。mingw3

。コンパイラをインストールしてPATHウィンドウに追加した後、次のコマンドを使用して.pyxファイルをコンパイルできます。

python setup.py build_ext -i --compiler=mingw32


2つ目は、python3.xとmsvcコンパイラを使用することです。



jupyterノートブックでcythonを実行する方法。



場合によっては、jupyterを使用してコードの動作を視覚的にテストする必要があります。毎回cmdでコードをコンパイルしないようにするために、jupyterセルでcythonを使用できます。

これを行うには、jupyterセルで実行してcythonをインポートします。

%load_ext cython


そして、次のセルでいくつかのコードを実行しましょう:

%%cython -a
import numpy as np

cdef int max(int a, int b):
    return a if a > b else b

cdef int chebyshev(int x1, int y1, int x2, int y2):
    return max(abs(x1 - x2), abs(y1 - y2))

def c_benchmark():
    a = np.random.rand(1000, 2)
    b = np.random.rand(1000, 2)
    
    for x1, y1 in a:
        for x2, y2 in b:
            chebyshev(x1, x2, y1, y2)


すべてが成功した場合、出力は次のようになります。




All Articles