Memo of "Cython-Speeding up Python by fusing with C"

References

-["Cython-Speeding up Python by fusing with C", O'Reilly Japan, ISBN978-4-87311-727-0] 1 ――Hereafter, it is called Cython book. -Cython documentation (Japanese translation) — Cython 0.17.1 documentation

Cython overview

--Cython is a programming language that combines Python with a static type system of C or C ++. --Creole programming language -[Creole languag-wikipedia] [2]: A portmanteau of multiple languages. --cython (lowercase c) is a compiler that converts Cython code into C or C ++ code. --Cython's strengths = Python expressiveness + dynamic personality + C language performance --Python code is Cython code as it is (with some exceptions) --Cython generates efficient C code by adding Cython keyword

Cython duality

--Yo: Compile Python to C --Yin: C / C ++ and Python interface --One of the areas where Cython is effective is "wrapping the C library in Python". (This is why I'm reading [Cython book] 1)

Importance of profiling

--Cython is not the first effective means if profiling reveals I / O bound / network bound --Cython is valid for ** CPU bound ** issues.

--Pareto principle --80% of program execution time is spent on only 20% of code. ――20% cannot be found without profiling -** Leverage Python's built-in profiling tools **

-** If the main processing is memory bound, I / O bound, and network bound, the difference between Cython and Python is small ** --Memory bound: Addition of elements in a huge array, etc. --I / O bound: Reading a file from disk, etc. --Network bound: Receive data from the network, etc.

About the type

--Bring a C type system into Python ⊃ Bring in the constraints of a C data type --Python → Infinite precision, C → Fixed precision --C is faster than Python ← → less flexible / general than Python

Compile and run Cython code

  1. Interactively with IPython
  2. Automatic compilation on import
  3. Compile separately using Python's distutils
  4. Integrate into the standard build system of make, CMake, SCons

--The standard method was "Compile separately using Python's distutils" (using distutlis and cythonize). ――You still need to find out if it really is.

Interactively with IPython

-When you do `` `% load_ext cythonmagic``` as in [Cython book] 1, the following error appears.

%load_ext cythonmagic
...\extensions\cythonmagic.py:21: UserWarning: The Cython magic has been moved to the Cython package
  warnings.warn("""The Cython magic has been moved to the Cython package""")

--When I saw Stack overflow, there was a solution

[Cython in Ipython: ERROR: Cell magic %%cython not found - Stack Overflow][4]

you should do:

%load_ext Cython

After that, the cython magic should work as expected.

――In [Cython book] 1,

Press the [Return] key twice in a row to exit the block

However, I could not get out. Line breaks endlessly like ↓.

In [6]: %%cython
   ...: def fib(int n):
   ...:     cdef int i
   ...:     for i in range(n):
   ...:         print(i)
   ...: 
   ...: 
   ...: 
   ...: 
   ...: 
   ...: 
   ...: 

-I got out with [Shift] + [Return]

In [7]: %load_ext Cython

In [8]: %%cython
   ...: def fib(int n):
   ...:     cdef int i
   ...:     for i in range(n):
   ...:         print(i)
   ...: # [Shift] + [Return]

-The reason you wait after pressing [Shift] + [Return] is because IPython compiles and loads the code block. --Generated source

....ipython\cython_cython_magic_ed5cb5261ba45739ef3878fbe8610564.c

/* "_cython_magic_ed5cb5261ba45739ef3878fbe8610564.pyx":3

*/


## Automatic compilation on import

 --Create the following fib.pyx
 (Although it is fib, it is not a Fibonacci sequence but an appropriate one for simplification.)


#### **`fib.pyx`**
```pyx

def fib(int n):
    cdef int i
    for i in range(n):
        print(i)

--Import pyx import allows you to import fib.pyx.

In [21]: import pyximport

In [22]: pyximport.install()
Out[22]: (None, <pyximport.pyximport.PyxImporter at 0x64f2cc0>)

In [23]: import fib

In [24]: fib.fib(10)
0
1
2
3
4
5
6
7
8
9

--Note that if tabs and spaces are mixed, a large number of errors will occur.

[2]: https://ja.wikipedia.org/wiki/ Creole language [3]:https://jp.vector.com/vj_xl_driver_library_jp.html [4]:https://stackoverflow.com/questions/36514338/cython-in-ipython-error-cell-magic-cython-not-found

Recommended Posts

Memo of "Cython-Speeding up Python by fusing with C"
[Learning memo] Basics of class by python
Visualization memo by Python
ABC163 C problem with python3
Twitter graphing memo with Python
Python started by C programmers
ABC188 C problem with python3
Regarding speeding up python (memo)
ABC187 C problem with python
Mass generation of QR code with character display by Python
[Python] A memo of frequently used phrases (by myself) in Python scripts
Solve ABC163 A ~ C with Python
I couldn't use tkinter with python installed by pyenv of anyenv
[Python] Operation memo of pandas DataFrame
Call C from Python with DragonFFI
Expansion by argument of python dictionary
python memo --Specify options with getopt
Input / output with Python (Python learning memo ⑤)
[Python] A memo to operate ROM created by GBDK with PyBoy
Getting Started with Python Basics of Python
Python3 compatible memo of "python start book"
Solve ABC168 A ~ C with Python
Life game with Python! (Conway's Game of Life)
10 functions of "language with battery" python
Roughly speed up Python with numba
Solved AtCoder ABC 114 C-755 with Python3
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Interval scheduling learning memo ~ by python ~
Separate display of Python graphs (memo)
Solve ABC158 A ~ C with Python
Implementation of Dijkstra's algorithm with python
Coexistence of Python2 and 3 with CircleCI (1.0)
"Scraping & machine learning with Python" Learning memo
Story of power approximation by Python
Speed up C / C ++ compilation with ccache
[Memo] Tweet on twitter with python
Basic study of OpenCV with Python
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
A memo organized by renaming the file names in the folder with python
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
Python learning memo for machine learning by Chainer Chapter 13 Basics of neural networks
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
[Unity (C #), Python] API communication study memo ③ Implementation of simplified login function
Python learning memo for machine learning by Chainer until the end of Chapter 2
[AtCoder explanation] Control the A, B, C problems of ABC184 with Python!
Explanation of production optimization model by Python
Basics of binarized image processing with Python
Get property information by scraping with python
Convert memo at once with Python 2to3
Memo to ask for KPI with python
Execute Python script with cron of TS-220
Save video frame by frame with Python OpenCV
Conditional branching of Python learned by chemoinformatics
Check the existence of the file with python
Algorithm learned with Python 8th: Evaluation of algorithm
"System trade starting with Python3" reading memo
[Python] Round up with just the operator
Clogged with python update of GCP console ①
A memo with Python2.7 and Python3 on CentOS