[PYTHON] I tried Cython on Ubuntu on VirtualBox

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

I tried Cython on Ubuntu on VirtualBox
I installed ROS on Ubuntu 18.04
I tried MLflow on Databricks
I tried installing the Linux kernel on virtualbox + vagrant
I tried AdaNet on table data
[Linux] I installed CentOS on VirtualBox
I couldn't input Japanese on Ubuntu 20.04
I tried playing Windows games using Steam Play on Ubuntu 20.04 LTS
I installed TensorFlow (GPU version) on Ubuntu
I stumbled upon installing sentencepiece on ubuntu
I tried scraping
I tried PyQ
Shebang on Ubuntu 20.04
I tried AutoKeras
I tried papermill
I tried django-slack
I tried Django
I tried spleeter
I tried cgo
I can't install Dask with pip on Ubuntu
I tried using Remote API on GAE / J
I tried running YOLO v3 on Google Colab
[Pythonocc] I tried using CAD on jupyter notebook
Change the resolution of Ubuntu running on VirtualBox
I tried LINE Message API (line-bot-sdk-python) on GAE
I tried playing with the calculator on tkinter
I tried to rescue the data of the laptop by booting it on Ubuntu
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried competitive programming
I tried running pymc
I tried ARP spoofing
I tried to implement Minesweeper on terminal with python
I tried using aiomysql
I tried Python> autopep8
Install PySide2 on Ubuntu
I tried python on heroku for the first time
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I got a UnicodeDecodeError when pip install on ubuntu
I tried using Hubot
I made a Python3 environment on Ubuntu with direnv.
I tried using ESPCN
I tried PyCaret2.0 (pycaret-nightly)
I tried using openpyxl
Install JModelica on Ubuntu
I tried a visual regression test on GitHub Pages
I tried using Ipython
I tried to debug.
I tried Kivy's mapview
I tried using ngrok
I tried using face_recognition
I tried to paste
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
I tried using PySpark from Jupyter 4.x on EMR
I tried using Jupyter
I tried using PyCaret