LoginSignup
2
1

More than 1 year has passed since last update.

Jupyter Notebook上でCythonを使用して高速化を行う

Posted at

本記事は
https://qiita.com/pashango2/items/45cb85390193d97523ca
を参考にしています。

環境

環境は以下の通りです。
windows10
jupyter core : 4.5.0
jupyter-notebook : 6.0.0
qtconsole : 4.5.1
ipython : 7.6.1
ipykernel : 5.1.1
jupyter client : 5.3.1
jupyter lab : 1.0.2
nbconvert : 5.5.0
ipywidgets : 7.5.0
nbformat : 4.4.0
traitlets : 4.3.2
cython 0.29.12
Python 3.7.3
適宜ご自身の環境に合わせて下さい。

手順

  1. Visual Studio 2019 バージョン16.3 Communityをダウンロードします。

https://visualstudio.microsoft.com/ja/downloads/
image.png
2. インストールします。以下のようにPython開発を選択します。
image.png (再起動を促すアイコンが出れば)コンピュータを再起動します。
3. bulid tools ダウンロードしインストールする。
image.png
image.png
上記c++ builder toolsを使用する。
(再起動を促すアイコンが出れば)コンピュータを再起動します。
4. 実行テスト

%load_ext Cython
%%cython
def cyfib(int n):
    a, b = 0.0, 1.0
    for i in range(n):
        a, b = a + b, a
    return a
import time

start = time.time()
cyfib(1000000)
t = time.time() - start
print (f"{t:0} [sec]")


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

start = time.time()
fib(1000000)
t = time.time() - start
print (f"{t:0} [sec]")

実行すると、通常の場合とCythonで高速化された場合の違いが分かると思います。

2
1
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
2
1