-["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 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
--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)
--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.
--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
--The standard method was "Compile separately using Python's distutils" (using distutlis and cythonize). ――You still need to find out if it really is.
-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
cdef int i
for i in range(n): # <<<<<<<<<<<<<<
print(n)
return n
*/
## 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