repository of my complied code to go with notes:
cython is python with statically typed python/C data types, so essentially a mash-up of performance benefits of C and high-level ease-of-programming of python
cython is python – almost any piece of python code is also valid cython code
cython is not a python to C translator, it is an alternative complier for python
python runtime environment; but rather than compiling to interpreted python code, it compiles to native machine codeC data types and explicitly declared python variable types (statically typed), code which manipulates python variable types and C variable types can be freely intermixed, with conversions occurring automatically wherever possiblepython operations is automaticpython’s exception handling facilities, including the
try-except andtry-finally statements, is available to in the midst of manipulating C datacython: figuring out how to compile the code file
cython uses ‘.pyx’ file extension instead of the typical python’s ‘.py’ extensionto compile with cython to ‘.c files, save python code to a '.pyx' extensioned file (fileToComplie.pyx` for example)
then, create a setup.py in the same directory with following code:
from distutils.core import setup
from Cython.Build import cythonize
setup(
ext_modules = cythonize("fileToComplie.pyx")
)
then, run this setup.py file to build the ‘.pyx' file to C:
$ python setup.py build_ext --inplace
this will create a ‘.c’ and a ‘.so’ (shared object) in the working directory along with a build directory
then, in the python CLI, do:
import fileToCompile
this will import the complied ‘.so’ into python as a package, and immediately run the script (displaying any print statement outputs)
import can be used like using functions from any other imported python packagecython data types have to be defined explicitly in the ‘.pyx’ file code to utilize the statically typed performance benefits
cdef or cpdef along with explicitly setting variable type enables compilation to machine level codestatically typed python-style cython variables:
cdef int x,y,z
cdef char *s
cdef float x = 5.2 (single precision)
cdef double x = 40.5 (double precision)
cdef list languages
cdef dict abc_dict
cdef object thing
def: regular python function interpretation, returns python objectcdef: cython only functions, can’t access these from python-only code (.py files), must access within cython, since there will be no C translation to python for thesecpdef: for cython and python compilation , will create a cython function and a wrapper for python
C only pointer like a C array, then cpdef will throw errorsC arrays (in lieu of numpy array) can be used through cython by using
from cython.view cimport array as cvarray
# cython: language_level=3 as the first line of the .pyx file to ensure complier knows python3 code is being compliedcython is a compiler which compiles python-like code files to C code
cython is not a python to C translator, the code runs within the python runtime environment; but rather than compiling to interpreted python code, it compiles to native machine code
cython data types have to be defined explicitly in the ‘.pyx’ file code to utilize the statically typed performance benefits
having compared a mean-squared-error function performance in cython and python, the cython function is 5 times faster on an average

fig: performance boost measurement with timeit
runtest.py in performance-tests folder in the work-bench repository to see performance results on your machine