Using C and Cython to speedup your python
Thanks this blog:
-
Compile C file to dynamic shared library
In Mac:
gcc -dynamiclib -o libfunctions.dylib functions.c
In Linux:
gcc -Wall -fPIC -shared -o libfunctions.so functions.c
-
Compile Cython file There are two ways to do this:
-
python3 setup.py build_ext --inplace
This will generate .o and .so file. Thenimport fibRec_Cython
in the caller -
using
pyximport
In the caller:import pyximport pyximport.install() import fibRec_Cython
-
-
python3 main.py
Result on my machine:
Python : 0.8736008979976759
Cython : 0.25456177699379623
C lib : 0.013578268997662235