I tried Cython on Ubuntu on VirtualBox.
$ sudo apt-get install cython
$ cython -V Check the version display with.
"Speeding up Python by fusing with Cython C" p3 The Cython version of the Fibonacci function Save as fib.pyx.
fib.pyx
# -*- coding: utf-8 -*-
def fib(n):
cdef int i
cdef double a=0.0, b = 1.0
for i in range(n):
a, b = a + b, a
return a
Put setup.py in p15 of "Speeding up Python by fusing with Cython C" in the same directory as fib.pyx.
setup.py
from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules=cythonize('fib.pyx'))
$ python setup.py build_ext --inplace To execute.
Launch the IPython console in the Spyder integrated environment.
in [1]: import fib
in [2]: fib.fib(1) Out[2]: 1.0
in [3]:fib.fib(90) Out[3]: 2.880067194370816e+18
in [4]: %timeit fib.fib(90) 10000000 loops, best of 3: 137 ns per loop
in [5]: import fib0
in [6]: %timeit fib0.fib(90) 100000 loops, best of 3: 4.78 µs per loop
In this example, the CPU bound leads to a significant improvement in processing speed. It seems good to investigate what is the bottleneck in each person's problem and consider using another method with or without Cython.
Reference: "Speeding up Python by fusing with Cython C" O'Reilly Japan
Postscript: The following site describes the procedure for running Cython on Raspberry Pi. "Electronic work tutorial" 1 Download Cython http://lumenbolk.com/?p=1054
Note: If you have to build scikit-learn or scikit-image from source code, you will probably run it as part of the build without being aware of Cython. Well, it's unlikely that you'll have to do that. In most cases, pip install should suffice.
Recommended Posts