[PYTHON] Cython to try in the shortest

Introduction

This article is a memorandum that describes the minimum contents required to try out a Python library called Cython.

I hope it helps people who say, "Cython seems to be a way to speed up Python, but I'm not sure what it is!"

Overview

Don't be afraid to misunderstand

Cython = A new programming language insanely similar to Python ** for creating Python libraries **

is.

There are many other ways to use it, but I think this level of recognition is sufficient if you just try it first.

Flow until using Cython

    1. Write processing in Cython
  1. Translate (transpile) the source code into C language
    1. Build and library transpiled C source code

Hello World guy

Let's try it right away.

The minimum required is two.

    1. Code written in Cython (hoge.pyx)

hoge.pyx


def tasu(a, b):
    return a + b

It's very similar to Python, it's just like Python. In Cython, you can also define functions with cdef and cpdef, and you can specify the type of ʻaandb`, but let's go without going deep because we are just trying.

  1. Code for transpile & build (setup.py)

setup.py


from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules=cythonize("hoge.pyx"))

There is a magical spell that ** does not ** the Cython code I wrote earlier.

When the above two are ready, transpile and build with the following command.

python3 setup.py build_ext -i

I think that some files have been generated, but the generated hoge.c is a transpile of hoge.pyx to C language. It is a library that is imported from Python and used by a person like hoge.cpython-36m-x86_64-linux-gnu.so.

All you have to do now is import hoge.cpython-36m-x86_64-linux-gnu.so and call the function you just created.

>>> import hoge
>>> 
>>> print(hoge.tasu(1,2))

It seems that Cython does not receive much of the benefits of speeding up unless you know how to use it and where to use it, but it is surprisingly easy if you just try it.


that's all. Thank you very much.

Recommended Posts

Cython to try in the shortest
Try to decipher the login data stored in Firefox
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Programming to fight in the world-Chapter 4
In the python command python points to python3.8
Try to introduce the theme to Pelican
Try to calculate Trace in Python
Try to put data in MongoDB
Try to extract the keywords that are popular in COTOHA
The fastest way to try EfficientNet
Programming to fight in the world ~ 5-2
The easiest way to try PyQtGraph
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
Try to model the cumulative return of rollovers in futures trading
Try to solve the shortest path with Python + NetworkX + social data
Try logging in to qiita with Python
Try using the Wunderlist API in Python
Try using the HL band in order
Try to face the integration by parts
Try to reproduce NumPy's add.at in Julia
In Jupyter, add IPerl to the kernel.
Try the new scheduler chaining in PyTorch 1.4
Various comments to write in the program
Try hitting the YouTube API in Python
Try hitting the Spotify API in Django.
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
Try to solve the fizzbuzz problem with Keras
How to use the C library in Python
Log in to the remote server with SSH
First steps to try Google CloudVision in Python
Try to implement Oni Maitsuji Miserable in python
Try adding fisheye lens distortion to the image
Try to calculate a statistical problem in Python
3.14 π day, so try to output in Python
Try auto to automatically price Enums in Python 3.6
Try to decompose the daimyo procession into Tucker
Try to separate Controllers using Blueprint in Flask
[Cloudian # 7] Try deleting the bucket in Python (boto3)
Try to solve the man-machine chart with Python
Try using the BitFlyer Ligntning API in Python
Twitter streaming client to enjoy in the terminal
To dynamically replace the next method in python
How to try the friends-of-friends algorithm with pyfof
Draw graphs in Julia ... Leave the graphs to Python
The trick to write flatten concisely in python
Try to calculate RPN in Python (for beginners)
How to get the files in the [Python] folder
Try to imitate Rails' _method parameter in webapp2
[Introduction to Algorithm] Find the shortest path [Python3]
Try implementing the Monte Carlo method in Python
Use pygogo to get the log in json.
Try to simulate the movement of the solar system
I want to display the progress in Python!
Try using the DropBox Core API in Python
Try posting to Qiita for the first time
9 Steps to Become a Machine Learning Expert in the Shortest Time [Completely Free]
Try adding an external module to pepper. For the time being, in requests.
Try to display the Fibonacci sequence in various languages in the name of algorithm practice