Demo of the optimisation of Pure Python code to its equivalent code in the Cython. This project demonstrates the scope of the potential improvements/enhancements in the Python code for faster runtime.
Based on https://www.peterbaumgartner.com/blog/intro-to-just-enough-cython-to-be-useful/
python -m venv testenv
source testenv/bin/activate
python -m pip install -r requirements.txt
- Pure Python Version:
solution.py
- Pure Python Version with compilation (~2X faster than pure python code):
solution_cython.pyx
- Python Code optimised by addition of C Types (~8X faster than pure python code):
solution_cython_typing_with_c_types.pyx
- Python code optimised by addition of C Types and Numpy array with memoryview (~31X faster than pure python code):
solution_cython_typing_with_c_types_numpy_memory_view.pyx
Demo iPython notebook that demonstrate each version: demo.ipynb
- Pure python code, e.g.,
solution.py
- Optimization Level 1: Copy the code in a file format
.pyx
to add support the Cython compilation support, e.g.,solution_cython.pyx
- Optimization Level 2: Convert the Python types to C types, e.g.,
solution_cython_typing_with_c_types.pyx
- Optimization Level 3: Use numpy array's memoryview, e.g.,
solution_cython_typing_with_c_types_numpy_memory_view.pyx
Compile the Cython Script: python setup.py build_ext --inplace
Fig.1 - Time Taken in Pure Python Code Execution Fig.2 - Time Taken in Pure Python Code Execution with Compilation Fig.3 - Time Taken in Python Code Execution with C-Types Fig.4 - Time Taken in Python Code Execution with C-Types and Numpy Array Memory View