LoginSignup
11
10

More than 5 years have passed since last update.

Cython on Raspberry Pi

Last updated at Posted at 2015-07-31

Pythonで開発しているプログラムをRaspberryPiのようにPCに比べて貧弱なCPUに移植する際には、遅さが目立ってしまう。

なるべく少ない労力で高速化できるかどうか、Cythonの状況を調べてみた。
(Scikit-learn, scikit-imageはCythonを用いて高速化をしている。それらが、RaspberryPiでも使えていることを考えると、Cythonの枠組みがRaspberryPiでも使えているはずだ。)

Cythonを使ってRaspberryPi上で高速化している事例は見つかった。
Cython自体のinstall方法はどうしているのか調査中だが、
sudo pip install cython
としているのが見つかった。

Speed of Python, at least for doing raw IO, is quiet disappointing. By using Cython (for the whole program), speeds becomes acceptable.

ボトルネックになっていて書き換えをする価値が高い関数を特定して
・単体テストを書く、・Pythonをベースとして、pyxファイルへの書き換えで高速化する。
(あるいは、C/C++言語で記述して、Cythonでラップすることで高速化する。)
・単体テストが成功していることを確認する。
のがよさそうです。

NEON命令を使う前のC/C++のソースと
NEON命令を使ったC/C++のソースとを
書いてARMでの高速化をはかるのがよさそうです。

もちろんCython以前の部分で最適化できることを実施しておくことが重要です。
python2.7で気軽にrange()関数を使ってしまいがちです。
xrange()で置き換えられる部分を置き換えます。
イテレータやジェネレータを使って、巨大なリストを生成させずに処理することが効果的だったりしています。

情報源

[1]:Raspberry, running Python
http://www.student.tue.nl/W/r.mientki/running_python.html

[2]:User’s Guide » Installation on Raspberry Pi
Install Cython from sources (debian package are outdated):
http://kivy.org/docs/installation/installation-rpi.html

11
10
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
11
10