2019年3月6日 星期三

[cython] Cython 不是一個 Python 2 C 的轉換器

Cython is a compiler which compiles Python-like code files to C code. Still, ‘’Cython is not a Python to C translator’‘. That is, it doesn’t take your full program and “turns it into C” – rather, the result makes full use of the Python runtime environment. A way of looking at it may be that your code is still Python in that it runs within the Python runtime environment, but rather than compiling to interpreted Python bytecode one compiles to native machine code (but with the addition of extra syntax for easy embedding of faster C-like code). 


你可以用來加速需要加速的緩慢 python module. 但它依然需要 Python 直譯器環境.

Using Cython consists of these steps:


  1. Write a .pyx source file
  2. Run the Cython compiler to generate a C file
  3. Run a C compiler to generate a compiled library
  4. Run the Python interpreter and ask it to import the module



Reference
https://cython.readthedocs.io/en/latest/src/userguide/numpy_tutorial.html