Cython
Cython
Tutorial: cython
Hello world
First, write the code in the file helloworld.pyx
|
Then, write a file setup.py
with codes below:
|
Now, let’s setup the Cython in the terminal/cmd
|
Compiling helloworld.pyx because it changed. [1/1] Cythonizing helloworld.pyx /home/ken/.local/lib/python3.8/site-packages/Cython/Compiler/Main.py:369: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/ken/test/helloworld.pyx tree = Parsing.p_module(s, pxd, full_module_name) running build_ext building 'helloworld' extension gcc -pthread -B /mnt/8A26661926660713/Conda/miniconda/compiler_compat -Wno-unused-result -Wsign-compare -DNDEBUG -fwrapv -O2 -Wall -fPIC -O2 -isystem /mnt/8A26661926660713/Conda/miniconda/include -fPIC -O2 -isystem /mnt/8A26661926660713/Conda/miniconda/include -fPIC -I/mnt/8A26661926660713/Conda/miniconda/include/python3.8 -c helloworld.c -o build/temp.linux-x86_64-cpython-38/helloworld.o gcc -pthread -B /mnt/8A26661926660713/Conda/miniconda/compiler_compat -shared -Wl,--allow-shlib-undefined -Wl,-rpath,/mnt/8A26661926660713/Conda/miniconda/lib -Wl,-rpath-link,/mnt/8A26661926660713/Conda/miniconda/lib -L/mnt/8A26661926660713/Conda/miniconda/lib -Wl,--allow-shlib-undefined -Wl,-rpath,/mnt/8A26661926660713/Conda/miniconda/lib -Wl,-rpath-link,/mnt/8A26661926660713/Conda/miniconda/lib -L/mnt/8A26661926660713/Conda/miniconda/lib build/temp.linux-x86_64-cpython-38/helloworld.o -o build/lib.linux-x86_64-cpython-38/helloworld.cpython-38-x86_64-linux-gnu.so copying build/lib.linux-x86_64-cpython-38/helloworld.cpython-38-x86_64-linux-gnu.so ->
Finally, import it in any python interpreter:
|
Hello World
Speed up the for loop
First, let’s create a file named example.pyx
with the following Cython code:
|
To compile and use the Cython code, you’ll need a setup.py file. Create a setup.py
file with the following content:
|
Next, compile the example.pyx file:
|
Now, you can test it in the python
|
0.001369476318359375 0.11698126792907715
When there are 1M loops, Cython is 85 times faster than python.
|
13.218583345413208 0.11868596076965332 0.19266653060913086
When there are 10000M loops, Cython is 85 times faster than python.