差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン最新のリビジョン両方とも次のリビジョン | ||
c_cpp:boost:boost_python [2020/08/27 17:16] – ともやん | c_cpp:boost:boost_python [2020/09/11 19:20] – ともやん | ||
---|---|---|---|
行 1: | 行 1: | ||
- | < | ||
- | < | ||
- | #result pre, #mincode pre { | ||
- | overflow: hidden; | ||
- | font-size: 10px; | ||
- | } | ||
- | # | ||
- | height: 250px; | ||
- | overflow: scroll; | ||
- | overflow-x: hidden; | ||
- | font-size: 10px; | ||
- | } | ||
- | #mintbl table { | ||
- | font-size: 12px; | ||
- | } | ||
- | .dokuwiki .plugin_wrap table { | ||
- | width: auto; | ||
- | } | ||
- | #logo { | ||
- | background-color: | ||
- | padding: 10px; | ||
- | width: fit-content; | ||
- | } | ||
- | #logo p { | ||
- | margin: 0; | ||
- | } | ||
- | </ | ||
- | </ | ||
====== Boost.Python - Boost Python Library ====== | ====== Boost.Python - Boost Python Library ====== | ||
+ | 本家: [[https:// | ||
+ | |||
**Boost Python Library** は、Python と C++ を連携させるためのフレームワークである。Dave Abrahams の C++ コンパイラだけで、特別なツールを使用することなく、C++ クラスの関数やオブジェクトを Python に素早くシームレスに公開できる。\\ | **Boost Python Library** は、Python と C++ を連携させるためのフレームワークである。Dave Abrahams の C++ コンパイラだけで、特別なツールを使用することなく、C++ クラスの関数やオブジェクトを Python に素早くシームレスに公開できる。\\ | ||
行 102: | 行 76: | ||
from random import random | from random import random | ||
import timeit | import timeit | ||
- | from superfastcode | + | from superfastcode_cpython |
- | from superfastcode_bst | + | from superfastcode_boost |
COUNT = 500000 | COUNT = 500000 | ||
行 211: | 行 185: | ||
※ここでは [[windows: | ※ここでは [[windows: | ||
< | < | ||
- | %USERPROFILE%\scoop\apps\python\current\include | + | $(USERPROFILE)\scoop\apps\python\current\include |
</ | </ | ||
+ | **※構成プロパティでは< | ||
{{c_cpp: | {{c_cpp: | ||
行 218: | 行 193: | ||
※ここでは [[windows: | ※ここでは [[windows: | ||
< | < | ||
- | %USERPROFILE%\scoop\apps\python\current\libs | + | $(USERPROFILE)\scoop\apps\python\current\libs |
</ | </ | ||
+ | **※構成プロパティでは< | ||
{{c_cpp: | {{c_cpp: | ||
上記のすべての設定が終わったら、プロジェクトのプロパティ ページの **[OK]** をクリックする。\\ | 上記のすべての設定が終わったら、プロジェクトのプロパティ ページの **[OK]** をクリックする。\\ | ||
行 261: | 行 237: | ||
static PyModuleDef superfastcode_module = { | static PyModuleDef superfastcode_module = { | ||
PyModuleDef_HEAD_INIT, | PyModuleDef_HEAD_INIT, | ||
- | "superfastcode", | + | "superfastcode_cpython", |
" | " | ||
0, | 0, | ||
行 267: | 行 243: | ||
}; | }; | ||
- | PyMODINIT_FUNC | + | PyMODINIT_FUNC |
return PyModule_Create(& | return PyModule_Create(& | ||
} | } | ||
行 329: | 行 305: | ||
※ここでは [[windows: | ※ここでは [[windows: | ||
< | < | ||
- | %USERPROFILE%\scoop\apps\python\current\include | + | $(USERPROFILE)\scoop\apps\python\current\include |
</ | </ | ||
+ | **※構成プロパティでは< | ||
{{c_cpp: | {{c_cpp: | ||
行 336: | 行 313: | ||
※ここでは [[windows: | ※ここでは [[windows: | ||
< | < | ||
- | %USERPROFILE%\scoop\apps\python\current\libs | + | $(USERPROFILE)\scoop\apps\python\current\libs |
</ | </ | ||
+ | **※構成プロパティでは< | ||
{{c_cpp: | {{c_cpp: | ||
上記のすべての設定が終わったら、プロジェクトのプロパティ ページの **[OK]** をクリックする。\\ | 上記のすべての設定が終わったら、プロジェクトのプロパティ ページの **[OK]** をクリックする。\\ | ||
行 362: | 行 340: | ||
} | } | ||
- | BOOST_PYTHON_MODULE(superfastcode_bst) | + | BOOST_PYTHON_MODULE(superfastcode_boost) |
{ | { | ||
using namespace boost:: | using namespace boost:: | ||
行 370: | 行 348: | ||
</ | </ | ||
- | ==== ソリューションの構成設定 ==== | + | ==== ソリューションの設定 ==== |
+ | **ソリューション エクスプローラー** より **PyHelloBoostPython** ソリューションを右クリックして、**[構成マネージャー...]**をクリックする。\\ | ||
+ | {{c_cpp: | ||
+ | **アクティブ ソリューション構成: | ||
+ | <WRAP prewrap 100% #mintbl> | ||
+ | ^ プロジェクト | ||
+ | |superfastcode_boost | ||
+ | |superfastcode_cpython | ||
+ | <wrap hi> | ||
+ | </ | ||
+ | {{c_cpp: | ||
+ | |||
+ | **アクティブ ソリューション構成: | ||
+ | <WRAP prewrap 100% #mintbl> | ||
+ | ^ プロジェクト | ||
+ | |superfastcode_boost | ||
+ | |superfastcode_cpython | ||
+ | <wrap hi> | ||
+ | </ | ||
+ | {{c_cpp: | ||
+ | |||
+ | **ソリューション エクスプローラー** より **PyHelloBoostPython** ソリューションを右クリックして、**[プロパティ]**をクリックする。\\ | ||
+ | **シングル スタートアップ プロジェクト**が **PyHelloBoostPython** であることを確認する。\\ | ||
+ | {{c_cpp: | ||
+ | |||
+ | ==== ソリューション内の Python プロジェクトで C++ 拡張機能 プロジェクトを参照する ==== | ||
+ | **superfastcode_cpython**、および、**superfastcode_boost** プロジェクトの Python 用 C++ 拡張機能は、同一ソリューション内であってもそのままでは Python から import できない。\\ | ||
+ | {{c_cpp: | ||
+ | |||
+ | **PyHelloBoostPython** プロジェクトの**[参照]**を右クリックして、**[参照の追加...]**をクリックする。\\ | ||
+ | {{c_cpp: | ||
+ | |||
+ | **superfastcode_cpython**、および、**superfastcode_boost** プロジェクトのチェックボックスをオンにして **[OK]** をクリックする。\\ | ||
+ | {{c_cpp: | ||
+ | |||
+ | 以下のように **PyHelloBoostPython** プロジェクトの参照に C++ 拡張機能 プロジェクトが追加されて、検索パス (**x64\Release**) にビルド出力の ***.pyd** ファイルが配置されるので Python プロジェクトから **import** することが可能になる。\\ | ||
+ | {{c_cpp: | ||
+ | |||
+ | ==== Python プロジェクトの実行 ==== | ||
+ | Visual Studio の画面で **Ctrl + F5**(デバッグなしで開始) キーを押して実行する。\\ | ||
+ | Python コードと C++ コードの実行速度のベンチマーク結果が表示される。\\ | ||
+ | <WRAP prewrap 100% #result> | ||
+ | <code powershell> | ||
+ | Running benchmarks with COUNT = 500000 x NUM_OF_EXEC = 30 | ||
+ | [tanh(x) for x in d] (Python implementation) took 1.930 seconds | ||
+ | [fast_tanh(x) for x in d] (CPython C++ implementation) took 0.248 seconds | ||
+ | [boost_tanh(x) for x in d] (Boost.Python C++ implementation) took 0.289 seconds | ||
+ | 続行するには何かキーを押してください . . . | ||
+ | </ | ||
+ | </ | ||
===== 参考文献 ===== | ===== 参考文献 ===== | ||
[[https:// | [[https:// | ||
[[http:// | [[http:// | ||
+ | |||
+ | [[http:// | ||
+ | [[https:// | ||
+ | [[https:// | ||
+ | [[https:// | ||